HTTP Request doesn’t include query - node.js

I am trying to send a POST request with query parameters using the request library. I have succeded in sending these requests, but I am still unable to include any working query.
I have followed instructions from other posts and concluded that I’ve tried everything on the internet that is related to my mission.
This is the code that does comply with what I found on the internet:
const request = require(’request’);
request.post({
url: 'https://example.com/endpoint',
qs: { with_counts: true } },
function (err, res, body) {
console.log(err, body);
}
);
This, however, does not work. The server does not recieve the query. I tried setting the body and json properties instead, but those didn’t work either.
I cant tell what I’m doing wrong, and I realize that it might not be possible for you to tell, but this is my last desperate attempt before giving up and switching to Python.

Related

Cookie not being set from node typescript request

I'm trying to set a cookie in a node request. I have tried using packages like js-cookie, cookie-js, cookie and cookie-manager but none work.
The way I have tried it is very straight-forward, whenever my endpoint gets called i.e. https://develop.api/sess/init, I set the cookie at the very beggining of the endpoint with the following code
import * as Cookies from 'js-cookie';
export const init = async (event: APIGatewayEvent, context: Context) => {
...
Cookies.set('hello', 'hello');
...
}
As my endpoint has an auth header, I can not directly call it into my browser URL due to missing permissions, so I tried generating the fetch function with postman and pasting it into my browser's console. The function is the following
var myHeaders = new Headers();
myHeaders.append("Referer", "accepted.referer.com");
myHeaders.append("key", "somekey");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
fetch("https://develop.api/sess/init", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
Once called, my request successfully returns the expected response, but it never shows a Set-Cookie header in the network section, neither shows my cookie in the Application section.
I have to mention that I also tried looking for the cookie when making the call within Postman, but it never sets it neither.
Also, I have tried starting the application in localhost, and I have a successful response, but my cookie is still not being set.
About the package showed in the code, I said I have tried it with different ones and their implementations, so I don't think a broken package is the problem.
I'm starting to think that I have a wrong idea about how cookies work, or that someway I am completely blocking the sending of cookies within my code.
Environment
If it helps in any way, my endpoint is being hosted in a AWS Lambda application.
I know this should be trivial, but being battling with it for a day now.
I finally answered my own issue. The key here is that I'm using AWS lambdas as the proxy, therefore, the headers I were using to send the cookies were wrong, I was sending the cookies with the endpoint instead of within the lambda. Let me explain myself.
I was adding 'Set-Cookie':'cookieKey:cookieVal' in the headers of the Postman Call that I was using to test both my local and develop environments.
Instead of that, I needed to send the request within the response of the lambda for the cookies to be registered.
Please check at the following links for similar cases ->
https://aws.amazon.com/blogs/compute/simply-serverless-using-aws-lambda-to-expose-custom-cookies-with-api-gateway/
https://forum.serverless.com/t/how-to-send-a-cookie-as-a-response/1312/7

tumblr.js createPhotoPost using multiple images url

I'm trying to user tumblr.js to create some photo post containing multiple images (from url). The method works fine when I use source (hence with a single url), but stop working when I try using data instead of source to send multiple photos at the same time.
My code is pretty basic for the moment but is still not working :
client.createPhotoPost('myblog.tumblr.com', {data: ['http://www.gstatic.com/webp/gallery/1.jpg'] },
function (err, data) {
console.log("CREATE PHOTO POST", err, data)
});
The error is API error: 401 Unauthorized which does not seem to be relevant.
Does anyone has already used this and has any idea how to fix it ?

NodeJS soap request "Invalid WSDL URL"

I'm trying to make a request to a web service with a URL similar to the following:
http://91.21.24.521:7831/XXXXX/WS/{companyName}/XXXXX/XXXX?wsdl
I've blanked out some information with the XXXX, but that's the basic structure of the URL. The company name actually has blank spaces. I've tried making the request with the blank spaces and filling them with %20, but neither work. The URL works fine in Soap UI, where I fill the blank spaces with %20.
In Javascript I'm using the "soap" NPM module. And my current code looks like this:
const auth = 'Basic ' + Buffer.from(`${user}:${password}`).toString('base64')
const options = {
wsdl_headers: {
'Authorization': auth,
forceSoap12Headers: true,
disableCache: true,
}
}
soap.createClient(url, options, function (err, client) {
console.log(err, client)
})
When I inspect the console log, I get an undefined client and an error that says Invalid WSDL URL.
I've tried removing the options, trying different version of the URL, changing the options object structure but nothing seems to work. I'me confused because the URL works fine in Soap UI and the web browswer, but doesn't work in this scenario. Is there something wrong with the code?
Thanks

How to POST image as form data using npm request?

I have a specific task: I need to download an image from the source URL and upload it to another host via POST request as multipart/form-data. I'm trying to use node.js request library but never succeed.
The following code doesn't send anything in the request body.
request.post({
url: uploadUrl,
formData: {
photo: request(imageUri)
}
}, function (err) {
if (err) console.error(err.stack)
});
I have tried posting directly through the form-data library, but it doesn't seem to work neither. How do I solve this without the creation of temp files?
As i said in my comment, you need to wait until you have the image to make the post request. If you wanted to pipe the streams, you could try something like this...
request.get(imageUri).pipe(request.post(uploadUri));
Hope that helps.
The problem turned out to be that my imageUri had query parameters in it. I think this is a bug in form-data library. Removing query parameters solved the problem.

Accessing QCMobile API

I'm currently tasked with accessing data from the Department of Transportations QCMobile API, located here.
I've made an account and have obtained my key. I've tried accessing it through Ajax calls, Node's Request and https modules, and now I'm just trying to get a response via Curl.
Every time I try to access it I get the same error: error 403, Forbidden.
My URL appears to be properly formed, as seen here:
https://mobile.fmcsa.dot.gov/qc/services/carriers/44110/basics?webKey=xxxx
When I run it from Node or from an Ajax call, I only get 403, Forbidden.
Here is my relevant Node code:
this.url = 'https://mobile.fmcsa.dot.gov/qc/services/carriers/' + dotNumber + '/basics' + '?webKey=' + this.webkey;
this.options = {
method: 'GET',
uri: this.url,
};
this.getDoTData = function() {
request(this.options)
.then(function (response) {
// Request was successful, use the response object at will
console.log(response);
})
.catch(function (err) {
// Something bad happened, handle the error
console.log(err);
});
}
When I run it via Curl, I get the same 403 with some extra detail:
curl: (56) SSLRead() return error -9806
I was wondering if anyone has any ideas as to if I'm accessing this API incorrectly. There doesn't appear to be much documentation, and the page on their site where you can submit technical questions seems to be broken.
Thanks for any insight.
This web service seems to be down for the time being. These problems started during the maintenance window on weekend of Nov 18th, and it has been in varying degrees of non-operation since then.
This is the response I got from FMCSA customer support today:
I do apologize about the inconvenience however, we have been experiencing technical difficulties with the App for a few weeks now. Our IT department is currently working on it however, at this time I do not have an exact time on when it will be fixed.

Resources