Thursday, September 1, 2022

[SOLVED] curl, API request works in Insomnia, but not in generated code for curl

Issue

I have request in Insomnia. When I send it via Insomnia, no problemo. But when I generate source code for curl, it shows me this.

curl --request POST \
  --url URL \
  --header 'Authorization: bearer XXX' \
  --header 'content-type: multipart/form-data; boundary=---011000010111000001101001' \
  --cookie JSESSIONID=XXX \
  --form datasetFile=@FOLDER/producthierarchyl1-1.zip

{"message":"Bad Request","logref":null,"path":null,"_embedded":{"errors":[{"message":"Required argument [Publisher datasetFile] not specified","logref":null,"path":"/datasetFile","_embedded":{},"_links":{}}]},"_links":{"self":{"href":"/data-api/public-api/v4/datasets/ingestion/","templated":false,"profile":null,"deprecation":null,"title":null,"hreflang":null,"type":null,"name":null}}}

Insomnia output is like this.

> POST PATH HTTP/2
> Host: URL
> user-agent: insomnia/2022.3.0
> cookie: JSESSIONID=XXX
> authorization: bearer XXX
> content-type: multipart/form-data; boundary=X-INSOMNIA-BOUNDARY
> accept: */*
> content-length: 407

* STATE: DO => DID handle 0x170404b14008; line 2077 (connection #27)
* multi changed, check CONNECT_PEND queue!
* STATE: DID => PERFORMING handle 0x170404b14008; line 2196 (connection #27)

| --X-INSOMNIA-BOUNDARY
| Content-Disposition: form-data; name="datasetFile"; filename="producthierarchyl1-1.zip"
| Content-Type: application/zip
| PK�QU�}�%+producthierarchyl1-1.csvUT  ΥΆ�b
| --X-INSOMNIA-BOUNDARY--

* We are completely uploaded and fine
* HTTP/2 found, allow multiplexing

Where is the catch?

Thanks.


Solution

Remove the --header 'content-type: multipart/form-data; boundary=---011000010111000001101001 option.

It really should not be used like this: curl manages that header and the boundary on its own so changing it should only be done in the rare event you truly want and need to fiddle around with it.

A more minor second thing: --request POST should be dropped from the command line, as it is superfluous and easily cause problems if you add --location later on.

Updated

This flaw is said to have been fixed in a later version of Insomnia



Answered By - Daniel Stenberg
Answer Checked By - David Goodson (WPSolving Volunteer)