HTTPS POST request is not passing any data - javascript

I am trying to replay a request to another instance of my API, so I am making a POST request as shown below. This works fine except for the fact that the data (JSON.stringify(req.body)) is not being passed. req.body on the other instance returns a blank map {} even though I have confirmed that JSON.stringify(req.body) returns what I expect. Is there something wrong with how I am passing the data with the POST request? Thanks in advance for any help.
var betaReq = https.request({
hostname: 'some-product-beta.cloudfunctions.net',
path: '/intercomEventsReplayed',
method: 'POST',
headers: {
'x-hub-signature': req.headers['x-hub-signature']
}
});
betaReq.write(JSON.stringify(req.body));
betaReq.end();

Related

Axios POST to endpoint, returns OPTIONS request

I am trying to make a post request from a form to an endpoint, the form works fine, and the db too. The main issue is that i am posting to this endpoind localhost:3001/professional with the body of the form as data
export const postPro = (data) => async () =>{
console.log(data)
axios({
method: 'post',
url: "http://localhost:3001/professional",
data: data
})
}
data itself is an object. This is what i get from the console after i press send:
console of backend returning options, I did the first 2 post with postman to see if it works the db, and indeed it does.
I am using Redux toolkits(first time i use it) and React Native
I try rewriting the post, like this:
axios.post("http://localhost:3001/professional", data)
then i try to console.log the data to see if reaches the axios.post and it does, i can see the log of the object data in the console.
Also i was thinking that has something to do with async await so i try to play with that a little, and it's the same result.

cURL command works, but Fetch API call returns 200 with 400 validation error in Response Payload

Having a huge issue I came across in sending a POST request using Fetch to get a URL shortened.
I am good and able to do a POST request by cURL command to this url shortener API:
Curl command
curl -d 'api_key=xxxxxxxxxxxxxx&url=https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch' http://fakeurlforexample/api/shorten/
Response
{"url": "https://fakeurlforexample/BdzfM3", "long_url": "https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch", "name": "BdzfM3"}
And I get this great response payload from the API.
But when I do this by Fetch with this code I provided below, I get a 200 OK and in the response payload I have a 400 validation error that I am missing the API key.
However, the request payload in the developer console shows that the parameters were passed on properly to the API (I think...)
{"api_key":"xxxxxxxxxxxxxxxxx","url":"https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch"}
Here is my code:
let get_url = 'http://fakeurlforexample.com/api/shorten/';
let request = new Request(get_url, {
method: 'POST',
body: JSON.stringify({'api_key':'xxxxxxxxx', 'url': 'https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch'})
});
fetch(request)
.then(function() {
console.log(request);
console.log(request.url);
})
Does anyone see the mistake I am making here?
Been beaten down by this for across hours upon hours this week now. Thanks for any help and assistance! And no, I can't easily transition the code to axios as it is right now. This is a demonstration so I'm really just trying to get it to work.
From the curl manpage Options section on -d, --data <data>:
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded. Compare to -F, --form.
Whereas with your request, you are sending a JSON object (Content Type: application/json):
let request = new Request(get_url, {
method: 'POST',
body: JSON.stringify({'api_key':'xxxxxxxxx', 'url': 'https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch'})
});
Since you know the API endpoint accepts application/x-www-form-urlencoded because the curl request succeeds, you can set the content type as application/x-www-form-urlencoded and send the body as a string:
let request = new Request(get_url, {
method: 'POST',
headers: new Headers({'content-type': 'application/x-www-form-urlencoded'}),
body: 'api_key=xxxxxxxxxxxxxx&url=https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch'
});
Maybe its because you are defining headers 2 times.

How do I sent additional data to javascript's fetch() function

I want to use fetch() to query an API endpoint which powers my search page. It returns a list of search results in JSON format.
I also want to pass to the API the current query submitted by the user. The old implementation used jquery and getJSON. Looking at the docs for getJSON, it says that I can pass in a data variable:
data
Type: PlainObject or String
A plain object or string that is sent to the server with the request.
Looking at the docs for fetch, I'm not sure how to pass in data as part of my request. So my question is, how do I pass in a string that will be sent to the server along with my request?
EDIT: I think I can append the query to the request URL, like "/search/?q=Something" and send that. Does anyone have a better way?
If you look at the Body section of the documentation on fetch, it lists several types of values you can use to specify the data sent in your request.
Example using FormData:
var fd = new FormData();
fd.append('q', 'Something');
fetch('/search', {
method : "POST",
body : fd
})
.then(...)
Note that you can't use the body option on GET or HEAD requests (which it seems you may be doing in your case). In that situation, you can build up the parameters using URLSearchParams:
var params = new URLSearchParams();
params.append('q', 'Something');
fetch('/search/?' + params.toString(), {
method: 'GET'
})
.then(...);
You can pass as below
fetch('/users', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
name: 'Hubot',
login: 'hubot',
})
})

IE8 changing GET request to POST: Angular $http

I am making call to a REST service from my AngularJS app using $http. The issue is whenever I make a GET request from IE8, it gets converted to POST request. Calls with other http methods (POST,PUT) work fine. This happens only with IE8.
Here is my code
```
var request = {method: method, url: url, data: payload};
var promise = $http(request) .then(function (response) {
return response;
});
```
Can someone please help. I have tried sending different types off data payload : null,undefined,empty object, some object. But nothing worked.
I think I have found the solution. We need to send empty string as payload. Or, use $http.get

HTTPS request with node.js?

I need to trigger a request in my node.js app. My app has a route and when it runs I am trying to hit a url that is built dynamically. So all I need is to trigger a RESt API call to somethinglike:
"https://www.domainname.com/sometext/"+ var1 +"/someothertext"
So I tried this:
var options = {
host: 'www.domainname.com',
port: 80,
path: '/1.0/'+var1,
method: 'GET'
};
// trigger request
request(options, function(err,response,body) {
.......
});
When I run this I get this error:
options.uri is a required argument
So, my goal here is to trigger the request that hits a dynamically built url. If I had a static url I could plug in the request, it would work fine.
Infact I tried to do this:
request("https://www.domainname.com/1.0/456", function(err,response,body) {
.......
});
and this works fine.
BUT I am trying to build the url (path) dynamically with var1 and that doesn't work.
Any suggestion on how to do this?
You need a URL or an URI in the options that you pass as the first arguments to the request function
And the reason that request("https://www.domainname.com/1.0/456",function(err,response,body) {
does not fail is because you are providing the url as the first argument
So change your options object to
var options = {
url: 'https://www.domainname.com/sometext/'+ var1,
port: 80,
method: 'GET'
};
You can try trimming the value in var1 like
var1 = var1.replace(/^\s*|\s*$/g, '');
That should remove the space.
Because the options argument that is taken by request isn't being given the correct formatted object.
The error you are receiving is because you need to send a url or uri as an option. This can be clarified here:
https://github.com/request/request
The following should do what you want:
So I tried this:
var options = {
url: "https://www.domainname.com/sometext/"+ var1 +"/someothertext"
method: 'GET'
};
// trigger request
request(options, function(err,response,body) {
console.log(response.statusCode);
});

Categories

Resources