Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting curl to fetch

I can successfully curl an endpoint and get a 200 response, but when I use the curl to fetch converter, the api complains about on of the body params. I don't have any control over the api so I'm not really sure what's going on there.

Here is my successful curl:

curl -v -X POST https://someurl -d 'param1=someValue' -d 'param2=somOtherValue'

Using https://kigiri.github.io/fetch/, suggests using following body in the fetch request:

"param1=someValue&param2=someOtherValue"

But using that gives me the response:

Param1 is not valid

Any idea on what the fetch body should look like to make it work just like the curl?

EDIT:

Converting the fetch back to a curl helps to understand the difference. So, this works:

curl -v -X POST https://someurl -d 'param1=someValue' -d 'param2=someOtherValue'

But this doesn't:

curl -v -X POST https://someurl -d 'param1=someValue&param2=someOtherValue'

This seems to be the case for this specific api, still I can't change the api so I would like to find the equivalent fetch body for the first curl

like image 692
Mike Avatar asked Oct 16 '25 05:10

Mike


1 Answers

you can use this to convert curl to fetch:

fetch("https://someurl", {
  body: "param1=someValue&m2=somOtherValue",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  method: "POST"
})

Working snippet

fetch("https://my-json-server.typicode.com/typicode/demo/posts", {
  body: "param1=someValue&m2=somOtherValue",
  headers: {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  method: "POST"
}).then(res => console.log(res));
like image 86
Mosè Raguzzini Avatar answered Oct 17 '25 19:10

Mosè Raguzzini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!