How to send form data in httpsync - node.js? - node.js

I am using httpSync module to fetch data from the server.
I have sample as below
var httpsync = require('httpsync');
var req = httpsync.request({
url: url,
method: "POST"
});
Note:
I am using httpSync because i want the request to behave synchronous.
I cannot add original code
How can i send the form-data to the server using httpSync
I had tried the following but it is not working
var req = httpsync.request({
url: url,
method: "POST"
form:{a:1,b:1}
});
Any suggestion will be grateful

httpsync module has an example for this.
Check this link
https://github.com/fengmk2/node-curl/blob/master/example/upload_file.js

Related

Put file to URL with Http Utils as multipart form encoded

Is it possible to PUT a file with Http Utils as multipart form encoded?
This is what I tried:
var response = $"{_baseUrl}{address}".PutBytesToUrl(File.ReadAllBytes(filePath), "image/jpeg", "*/*",
requestFilter: req =>
{
req.Headers["x-aws-acl"] = "private";
req.Headers["content_type"] = "image/jpeg";
req.Headers["X-Shopify-Access-Token"] = _accessToken;
});
The request goes through with 200 but the API (Shopify) doesn't have the image.
I tried running the request in postman and with postman the request works and shopify has the image after.
I used webhook.site to see what the different was in http utils and postman and it seems postman is sending multipart encoded form.
Here are http utils headers being sent that result in no image:
Here are postman headers:
Any way to get http utils to send the image as multipart form data?
To Upload Files as multipart/form-data you would need to use the UploadFile APIs which accepts an overload for specifying which HTTP Method to use, e.g:
var webReq = (HttpWebRequest)WebRequest.Create("http://example.org/upload");
webReq.Accept = MimeTypes.Json;
using var stream = uploadFile.OpenRead();
webReq.UploadFile(stream, uploadFile.Name, MimeTypes.GetMimeType(uploadFile.Name),
method:"PUT");

use node.js send request for baidu map API get wrong response?

Im trying to use node.js to send http requst and call baidu map API.
my code in blow:
If you past the url and use browser directly, it will give right response in right format.
But when I use node to send request, I get problem.
var request = require('request');
request(
{ method: 'GET',
uri: 'http://api.map.baidu.com/place/v2/suggestion?query=beijing&region=131&output=json&ak=****hLQKu9ap9fPq5N1ExF1Kk7xe5Eah'
}
, function (error, response, body) {
res.json({
res:response
})
}
)
Meanwhile, if I change the url contains some Chinese like:
http://api.map.baidu.com/place/v2/suggestion?query=北京理工大学&region=北京&output=json&ak=****hLQKu9ap9fPq5N1ExF1Kk7xe5Eah
In node.js it will give status code 400 and totally wrong response.
you must encode your uri with encodeURI
uri: encodeURI('http://api.map.baidu.com/place/v2/suggestion?query=北京理工大学&region=北京&output=json&ak=3104hLQKu9ap9fPq5N1ExF1Kk7xe5Eah')

koa.js streaming response from remote url

I want to create a koa route that acts like a proxy for another url, which delivers a file that is usually a few dozens of Megabytes.
Therefore I would like not to block when making the response. I am using this.body = yield request.get(url); currently, where request is the [co-request]1 module.
How do I stream the response back to the client ?
Edit :
I am now doing the following :
var req = require('request');
//...
this.body = req(url).pipe(fs.createWriteStream(this.params.what));
If I paste the url in my browser, I get a file just fine.
However if I get a Error: Cannot pipe. Not readable. in my route.
Turns out the solution was simply :
var req = require('request');
//...
this.body = req(url);
This is because this.body has to be a readable stream, which req(url) returns. Thanks to #danneu for the explanation.

Can I send a GET request with cookies in the headers in Node?

In a browser, if I send a GET request, the request will send the cookie in the meanwhile. Now I want to simulate a GET request from Node, then how to write the code?
Using the marvelous request library cookies are enabled by default. You can send your own like so (taken from the Github page):
var j = request.jar()
var cookie = request.cookie('your_cookie_here')
j.add(cookie)
request({url: 'http://www.google.com', jar: j}, function () {
request('http://images.google.com')
})
If you want to do it with the native http:request() method, you need to set the appropriate Set-Cookie headers (see an HTTP reference for what they should look like) in the headers member of the options argument; there are no specific methods in the native code for dealing with cookies. Refer to the source code in Mikeal's request library and or the cookieParser code in connect if you need concrete examples.
But Femi is almost certainly right: dealing with cookies is full of rather nitpicky details and you're almost always going to be better off using code that's already been written and, more importantly, tested. If you try to reinvent this particular wheel, you're likely to come up with code that seems to work most of the time, but occasionally and unpredicatably fails mysteriously.
var jar = request.jar();
const jwtSecret = fs.readFileSync(`${__dirname}/.ssh/id_rsa`, 'utf8');
const token = jwt.sign(jwtPayload, jwtSecret, settings);
jar.setCookie(`any-name=${token}`, 'http://localhost:12345/');
const options = {
method: 'GET',
url: 'http://localhost:12345',
jar,
json: true
};
request(options, handleResponse);

Setting request header in $.ajax

I need to make a request to a site which has HT Access enable. The str below has the username:password
Below is the code I am using
var str='msft:sks*';
var bytes = Crypto.charenc.Binary.stringToBytes(str);
var base64 = Crypto.util.bytesToBase64(bytes);
var auth='BASIC '+base64;
$.ajax({
url : 'http://mft.sta.com/',
method : 'GET',
async:false,
beforeSend : function(req,settings) {
req.setRequestHeader('Authorization', auth);
}
});
I am getting 401 authentication required error in FireBug.
But when i tried in POSTER plugin I am able to get the response. I copied the value of 'auth' in the above function and set it in the requester header for the key 'Authorization' in POSTER and it is working like a charm. But failing in Firefox or IE.
Any possible solution or workarounds.
Thanks in Advance.

Resources