I have tried every way I can think of to format this cURL request into something I can do server side on node and I keep getting back an invalid credentials response. I know the creds are valid so I must come to the conclusion that the code is making the wrong type of request.
cURL request:
curl --request GET \
--url https://api.timekit.io/v2/bookings\
--header 'Content-Type: application/json' \
--user : api_key_dfagafrgaegrfareVhROys9V1bUJ1z7
my format:
var options = {
url: 'https://api.timekit.io/v2/bookings',
headers:{
"Content-Type": "application/json",
'user': APP_KEY
},
method:'GET'
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
I think you mean --user user:password in your curl command.
When you pass --user to curl, it sends it to the server as an basic authorization header (with the user:password encoded using base64). You can try this by running it against httpbin.org:
curl --request GET --url http://httpbin.org/headers --header 'Content-Type: application/json' --user foo:bar
{
"headers": {
"Accept": "*/*",
"Authorization": "Basic Zm9vOmJhcg==",
"Connection": "close",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.61.1"
}
}
As you can see, they received the header "Authorization": "Basic Zm9vOmJhcg==" where Zm9vOmJhcg== is the base64 encode of foo:bar
Related
I'm trying to perform an API call to Squarespace using node-fetch.
const FormData = new FormData();
formData.append(
"file",
"testimage.jpg",
fs.createReadStream("testimg.jpg")
);
const send_img = await fetch(
"https://api.squarespace.com/1.0/commerce/products/" + img_id + "/images",
{
method: "POST",
headers: {
Authorization: "Bearer " + process.env.SQUARESPACE_API_KEY,
"User-Agent": "foobar",
"Content-Type": "multipart/form-data",
},
body: formData,
}
);
After performing this call I was getting the error
{
type: 'INVALID_REQUEST_ERROR',
subtype: null,
message: "Expected exactly one file part named 'file', but found none.",
details: null,
contextId: 'IEBROFREUTEWDREAYEAF'
}
The following cURL call successfully uploads the image
curl "https://api.squarespace.com/1.0/commerce/products/63ab0ff8796311649603ee49/images" \
-i \;
-H "Authorization: Bearer <myAPIKey>" \
-H "User-Agent: foobar" \
-H "Content-Type: multipart/form-data" \
-X POST \
-F file=#testimg.png
What if -F and what is the equivalent of it in a fetch call?
Squarespace API reference:
https://developers.squarespace.com/commerce-apis/upload-product-image
How to post Json data via node js either with form-data or via request ?
Using form-data. It does not allow to send custom headers and if replaced bodyParams.getHeaders() with custom headers it does not send request data
https://www.npmjs.com/package/form-data
const smsResponse = await request.post(url, bodyParams, {headers:bodyParams.getHeaders()})
Using Request it does not allow to send parameters
require('request');
const subscription = await request.post(url, body, {headers:{'Accept':'text/html'}})
Postman curl request It works via postman. tried to use postman nodejs request code but it fails
curl --location --request POST 'https://personalsite.com//test.php' \
--header 'accept: text/html' \
--header 'SECRET-TOKEN-MESSAGE: dataforport' \
--header 'Content-Type: application/json' \
--header 'Cookie: PHPSESSID=1d2shuebo7lal8sn2itgppvfk4' \
--data-raw '{
"mobileno": "888888888",
"messagetext": "Test message"
}'
Tried but it did not worked
Node.js: How to send headers with form data using request module?
Use new Headers() to build your header.
https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
var myHeaders = new Headers();
myHeaders.append('Accept', 'text/html');
var myInit = { method: 'POST',
headers: myHeaders,
mode: 'cors',
cache: 'default',
body: JSON.stringify({coffee: 'yes...'})
};
var myRequest = new Request('https://personalsite.com//test.php',myInit);
fetch(myRequest).then(function(response) {
console.log(response)
});
I am trying to send Https post request using NodeJS, but getting back 'Bad request'. At the same time when I send the same request via curl everything is fine. Can you help to fix the Node code:
var options = {
host: 'api.wit.ai',
port: 443,
path: '/converse?v=20170611&session_id=125abc&q=Hi',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer <token>'
}
};
var req = https.request(options, function(res) {...}
The curl query:
curl -XPOST 'https://api.wit.ai/converse?v=20170611&session_id=125abc&q=Hi' \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H 'Authorization: Bearer <token>'
With http.request() your post data is not to be sent in the query string. If you include a query string, it is sent as a query string. Your post data needs to be sent with req.write(data). See the code example in the doc.
Probably your server is returning that error because there is no data in the body of the POST.
I am trying to transform this curl command
curl '<url>' -X POST \
--data-urlencode 'To=<phone>' \
--data-urlencode 'From=<phone>' \
--data-urlencode 'Body=<message>' \
-u '<user>:<pass>'
into this Node.js code
var request = require('request');
var options = {
url: 'url',
method: 'POST',
auth: {
'user': 'user',
'pass': 'pass'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
I don't get how I can add the --data-urlencode option in the Node.js version of this code.
From curl documentation :
--data-urlencode
(HTTP) This posts data, similar to the other -d, --data options with
the exception that this performs URL-encoding.
So you would use form option to send form URL encoded like this :
var options = {
url: 'url',
method: 'POST',
auth: {
'user': 'user',
'pass': 'pass'
},
form: {
To: 'phone',
From: 'phone',
Body: 'message'
},
headers: {
'Accept': '*/*'
}
};
Note that you can use request-debug to show the actual request where you could check that body is :
To=phone&From=phone&Body=message
And to show the actual data sent by curl use as stated here, use --trace-ascii /dev/stdout :
curl '<url>' -X POST --data-urlencode "Body=<message>" -u <user>:<pass> --trace-ascii /dev/stdout
Here's the cURL request which it works:
curl -H
'X-New-ID: weR1RRzRw3R3R3Rz1'
-H 'Brand: 1'
-H 'X-Device-Version: 4.02'
-H 'X-Device-Source: 6'
-H 'Accept-Language: en-US'
-H 'Content-Type: application/json'
-H 'User-Agent: Dalv1k/2.1.0 (Linux; U; Andr0id 5.1; Go0gle Nexus 10 - 5.1.0 - API 22 - 2560x1600_1 Build/LM227D)'
-H 'Host: api.autoigs.com'
--data-binary '{"areaId":10,"cityId":1,"countryId":1,"kickId":0}' --compressed 'https://api.autoigs.com/apiAndroid/v1/kicks'
Unfortunately, I am not able to figure that out as well (like curl from cli) Though I would like to use nodejs to send this request. How do i do this?
Using the request package, you can do it like this:
const request = require('request');
request.post({
url: 'https://api.autoigs.com/apiAndroid/v1/kicks',
form: {"areaId":10,"cityId":1,"countryId":1,"kickId":0},
json: true,
headers: {
'X-New-ID': 'weR1RRzRw3R3R3Rz1',
'Brand': '1',
'X-Device-Version': '4.02',
'X-Device-Source': '6',
'Accept-Language': 'en-US',
'User-Agent': 'Dalvik/2.1.0 (Linux; U; Android 5.1; Google Nexus 10 - 5.1.0 - API 22 - 2560x1600_1 Build/LMY47D)',
},
}, function(err, response, body) {
if(err) {
console.error('error', err);
// handle error!
return;
}
// body contains response body
console.log(body);
});