Getting a 400 when using http client.request in nodejs - javascript

I'm trying to fetch some data from a server using nodejs. I would like to send a POST data. There are two things that I would like to know about.
How do I send the POST data?
Whatever request I make using GET or POST, I always get a 400-BadRequest error. Have been searching through to solve this the whole day. Was not able to solve.
I'm sending the POST data now like request.write(JSON.stringify({key:"value"})); .. for which I'm always getting a 400 to whatever site I try this. Even on apache running at 127.0.0.1 on a php file that accepts the POST data.

This question was answered in another SO thread: How to make an HTTP POST request in node.js?
Essentially:
use require('http');
set 'Content-Type': 'application/x-www-form-urlencoded' in the options
In the callback, use res.on('data', ...) to transfer the posted info.

Related

How to send DELETE request using browser in javascript

I have to use Postman to send POST PUT and DELETE request to my server, is there any way I can hit POST request through browsers directly, using browser front end only
I Have a form that on submits should delete data from my server but using a form I can send only the GET or POST method only I know I can do it with the POST method, but I need to do it by DELETE.
You may be able to send a "DELETE" request via Ajax, the question should be will your server understand it.
While DELETE is a http method, it's not a generally supported request type in most back-end frameworks, rather you generally "overload" a POST request while setting the action as delete via a hidden form field.
So depending on your back-end framework, you would need to consult their documentation, but generally its accomplished as described above.
As for performing a POST request by URL alone, in a browser there is no way that I'm aware of, you need some tool, to be able to set headers to be able to form a POST request, such as JavaScript, curl etc
MDN has a great introduction to the topic
The HTTP POST method sends data to the server. The type of the body of the request is indicated by the Content-Type header.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST

Can someone elaborate on exactly what is being asked?

We're setting up a basic http web server using node. We're coding in Java Script if this helps. I don't understand exactly what is being asked of us. Windows using Gitbash.
"Return the file to the user by setting the correct Content-Type for the file and writing it to the response"
That is word for word what is asked in the lab.
If someone could explain I'd appreciate it.
Whenever someone makes a request to a server, the server answers the request with a header and a body. The body usually contains the data you see on the browser and the header contains information about the type of content the server is returning.
When the server returns a file, it has to set this information on the header, otherwise the browser would expect the content to be just text. This information on the header that describes the content is called Content-Type.
As pointed out by Elliott Frisch, this web page contains a list of the suported mime types that your server should be returning: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
Basically it's a header sent in the request in addition to the request body. If you're sending say...video content to a server how is it supposed to know that from say an image?
There are several different kinds that can be seen here though usually you don't need content-type headers for things like POCOs, POJOs, and raw text. Here is a list of all the types
What are all the possible values for HTTP "Content-Type" header?
Here is how to change them in a fetch request from react (since you mentioned javascript):
fetch(url, {
method: "POST",
body: data,
headers: {
"Content-Type": "application/json"
},
credentials: "same-origin"
})
this would send a json data payload (you have to turn it into json manually) in a post request.
Also as an aside your post was tagged "java"....javascript and java are not the same thing.

why gunicorn sends HTTP 500 for json request which formed using obj[attr]

I want to know what is the different between obj.attr and obj["attr"].
Reasoning i am asking this question is, in one of the project i worked, we were invoking a post Rest API to send data in json format. Every time we used obj["attr"] to prepare json request and send to* backend server (gunicorn)*, Server replied with HTTP 500 291 error code. and when used obj.attr=value to prepare json request it worked.
when i replaced backend server of gunicorn with nodejs/SOAP Mockserver it worked

Express.js Node.js - OKTA getting user and groups info

I'm trying to get the user info by using /users/:id as well as the user's group info /users/:id/groups using OKTA API and the problem is that the response is sent as a string format and I need to JSON.parse() it before using, which obviously is an extra job. Did anyone faced the same issue?
The solution was similar to the Noob Coder's comment, I've used the request middleware to make http requests, it gives you an request configuration option {json : true} which automatically
sets body but to JSON representation of value and adds Content-type: application/json header. Additionally, parses the response body as JSON.

nodejs XHR alternative for http request

I need to use an API for an application I am builiding but the API doesn't allow for CORS, how else can I make GET and POST requests to the website without using XHR. I've been looking at websockets and socket.io but it doesn't seem like I can make http requests with them.
My other solution is creating using PHP or curl to make the requests but I feel like that isn't a good a idea.
Edit: More info
The API I want to use is https://bitcoinindex.es/api
I want to grab the exchange prices that are listed and list them from greatest to least.
I was using AngularJS to make the first API request here is my code
$http.get("https://bitcoinindex.es/api/v0.1/coinbase/usd/btc/last").success (JSON)
As a PHP developer moving from a traditional LAMP stack to angularJS and nodejs there were a few assumptions I made that were wrong.
All Http request are the same
Now of course I knew that there was GET, POST, and PUT but I thought a get request was the same across the board, this is not true. There are traditional request made from the backend of an application from a server using libraries such as curl. These http request are made before a webpage loads.
With AngularJS request are made from the front-end, meaning after the webpage is loaded the request is made. This is called XHR and is supported by all modern browsers, the thing with XHR is that it can leave user data vulnerable so it only works if the request is made to a location on the same server. If you're making a request to a different server then CORS needs to be enabled by the server response is coming from and can be configured through the access-control-allow-origin header.
NodeJS is some type of front-end hippy code
Again coming from PHP I didn't understand that nodeJS is an actual server just like an apache server, and can make the traditional request mentioned in the first section.
Making request
Making nodeJS request is very simple with the nodejs request library.
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Print the google web page.
}
})
If you want to do some request from server side using nodejs some module like Request could help you.

Categories

Resources