I can successfully curl an endpoint and get a 200 response, but when I use the curl to fetch converter, the api plains 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 /, suggests using following body in the fetch request:
"param1=someValue¶m2=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¶m2=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
I can successfully curl an endpoint and get a 200 response, but when I use the curl to fetch converter, the api plains 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¶m2=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¶m2=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
Share Improve this question edited Jul 2, 2019 at 8:22 Mike asked Jul 2, 2019 at 8:01 MikeMike 6121 gold badge11 silver badges29 bronze badges 7-
Maybe try just adding the params to the URL:
https://someurl? param1=someValue¶m2=someOtherValue
, and POST to that URL – sideshowbarker ♦ Commented Jul 2, 2019 at 10:08 -
When you give
-d 'param1=someValue¶m2=someOtherValue'
to CURL, it’s going to sendsomeValue¶m2=someOtherValue
as the value forparam1
. To send multiple params with curl, you need to specify a separate-d
option for each param. That’s why-d 'param1=someValue' -d 'param2=someOtherValue'
works and-d 'param1=someValue¶m2=someOtherValue'
doesn’t. – sideshowbarker ♦ Commented Jul 2, 2019 at 10:10 - @sideshowbarker: ok, so how do I do this in a javascript fetch? When using the curl to fetch converter and then converting it back to a curl again, I end up with the wrong curl. So what should the fetch body look like? – Mike Commented Jul 2, 2019 at 11:12
-
fetch("https://someurl?param1=someValue¶m2=someOtherValue", {method: "POST"})
is what it would look like. With no request body and no additional request headers needed. – sideshowbarker ♦ Commented Jul 2, 2019 at 12:21 - @sideshowbarker: doesn't -d mean that it is to be sent in the post body? – Mike Commented Jul 2, 2019 at 13:18
3 Answers
Reset to default 7you 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./typicode/demo/posts", {
body: "param1=someValue&m2=somOtherValue",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
method: "POST"
}).then(res => console.log(res));
I use postman client to convert api request between languages.
- Import -> Raw text (paste your curl mand here)
- Code -> select your language
I personally use this online cURL Converter It support multiple programming languages such as python, php, ruby, etc.
Curl Snippet
curl -d "param1=value1¶m2=value2" -X POST http://localhost:3000/data
Fetch Snippet
fetch("http://localhost:3000/data", {
method: "POST",
body: new URLSearchParams({
param1: "value1",
param2: "value2",
}),
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743665216a4486857.html
评论列表(0条)