I've a piece of code that sends a post request to my backend. Everything works fine for me.
However I have a vendor that are overseas facing a weird error. When they render my Vue Web Application, the POST request made to my backend has an additional "/" in between the parameters.
And i really do not know why it keeps happening at their end but not on me. We are both using the same browser.
Below is the library and part of the code that makes a request to my backend.
import axios from "axios";
export const HTTP = axios.create({
baseURL: _baseURL,
headers: {
"Content-Type": "application/json",
'Accept': "application/json"
},
timeout: 30000
});
HTTP.post(
`/someotherparams/restapiparams/${this.someinput}/someparameter/${this.moreinput}`,
{
something: "some text"
})
At the Vendor's End when i check the network of the browser, it shows that it is firing the following :
"BaseUrl/someotherparams/restapiparams/${this.someinput}//someparameter/${this.moreinput}"
As you can see there's an additional "/". Advance thanks for some guidance.
Note*: I've check the codes many times, the data in the data base for the this.someinput (does not contain '/') values. It works well for me, it did occur once from my support team. but the vendor is facing this issue every time.
Related
Apologies if I sound uneducated on the matter, I have never used JS until a few days ago and don't have much server/API experience.
Essentially I am using nextauth.js to authenticate a users twitter account and allow them to sign into an app. Next.js is providing them with a sign in option and once they sign in and authorize the app I now have access to their api key/ secret.
I also created an app in node.js that will allow me to change their twitter header for them. This runs on a separate port. I need to be able to send the key/secret I get from the next.js app to the node server so that I can use these keys to make the changes to their account. I am unsure of how to do this and what type of requests are required.
Ive spend a good amount of time looking into online resources but I haven't been able to conceptualize it fully. Any help/resources/explanations would be very appreciated! Thank you in advance!
if you have access to secret key you wanna send on the client-side you can simply use fetch
const response = await fetch('https://yourbackend.url/here', {
method: 'POST', // make sure the method is set to POST
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
},
body: JSON.stringify({secretKey: "yourseceretkeyhereaslkdaasldkaslkdjlask"}) // send the secret as payload in the body
}).then(res => res.json())
.catch(console.log);
catching the payload on your server will totally depend on what kind of server you have setup.
I want to get the IP which is used in this request to the URL. I am getting the response required( HTML of the webpage ). Since I want to try to dynamically change Ips using AWS lambda when making a request, I first wanted to see what IP is actually used. Is there a way to get that? I am using 'request-promise' module for this.
NodeJS Code
const options = {
method: 'POST',
"rejectUnauthorized": false,
url: URL,
formData: {
data:data
}
}
const response = await request(options)
I solved this issue by making an API which makes request to my other NodeJS server. and then printing the IP in the second server.
Also AWS Lambda in default state have changing Ips
Setup: NodeJS / Express / Helmet API running on Azure App Service, CORS set to allow * so should be all fine.
Client web setup running locally on NodeJS (http://localhost:3000)
I'm trying to POST some JSON from the web client to the API for login validating purposes. Works for some requests and not others, seemingly depending on what actual values I am posting.
I've got a bit of code that uses the Fetch API to send login details and validate them, as follows:
const response = await fetch(`${baseUrl}/auth/validate`, {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({EmailAddress: email, Password: password}),
}).catch(error => {
alert('Unable to check login');
});
If the value of email and password are both just me leaning on the keyboard, say 'skdjflsjdf' then it's all fine - the server gets my POST, validates that the login is rubbish and bounces back my error. It allows it because I get the Access-Control-Allow-Origin: * header back, so Chrome is happy.
If the value of email is an actual email address, I don't get the Access-Control-Allow-Origin: * header back and Chrome gets very upset of course.
I can't even begin to imagine how I've gone wrong here and I certainly don't seem to be able to find the right phrase to Google for a sensible answer. I'd really appreciate any help... Thanks.
request 2.83.0
node 8.11
yarn 1.5.1
Operating System: Windows Server 2012 R2
We're using request to power our proxy. It works well for all requests, and we're even transferring big files (400MB), all methods work and etc.
We create our options object like this:
let options = {
method: req.method,
url: url,
headers: proxyHelper.getHeaders(req.headers)
};
// The auth header needed to make the request
options.headers['Authorization'] = `Bearer ${token}`;
And then send it to request.
request(options, (err, responseFromApi, body) => {
logger.log("Body from API")
logger.log(body)
}).pipe(res);
The problem happens on requests that return "Bad Request" in the body, (status code 400).
The logger loggs the correct body, but pipes the wrong body to the client. The client only receives
"Bad Request". Instead of a complete JSON object that contains the reason for the error.
We use iisnode (a plugin for IIS) to run Node. It works locally when we run iisnode, but when we deploy it, it doesn't work.
Any ideas? Is it request? Is it pipe? Is it iisnode?
After a lot of struggling and research and breaking down the the whole node app, it was in the end IIS that took the body and and replaced its content with the statusText...
Update: Fixed. It looks like the request was coming back as a 503 (as it should), then my app refreshed and displayed the non-error message: "Fetch failed loading". I just wasn't seeing the response because of the refresh.
I am not able to make a fetch request from my locally-hosted Create-React-App to my Heroku-hosted Node server.
My Node server has CORS enabled. If I make a POST request via Postman, I get an appropriate response (503, because currently there is no database hooked up, so the data is not being saved. If I should be sending back a different response, let me know). My Postman request has 'application/json' as the content-type, no authorization, and a body of { "rating": "5", "zipcode": "0" }.
However, when I make a POST request from my React app, I get a message in my console: "Fetch failed loading: OPTIONS "https://shielded-gorge-69158.herokuapp.com/feedback"." There is no associated error, only the message. There is no information about the request in my Network panel.
The fetch request works when I do it locally, from localhost:3000 (my app) to localhost:5000 (my server). It only fails when I try to make the request to the (otherwise identical) server hosted on Heroku.
This is what the fetch request looks like:
return fetch('https://shielded-gorge-69158.herokuapp.com/feedback', {
method: 'POST',
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify({ rating: userRating, zipcode: userZip })
}).then(res => {
if (!res.ok) {
throw new Error('Error:', res.statusText);
}
return res;
}).catch(err => console.error(err));
Edit: I'm continuing to research and it seems like Postman shouldn't/doesn't make preflight requests (https://github.com/postmanlabs/postman-app-support/issues/2845). So perhaps that is the issue — but why would the request be working when my server is local, but not when it is hosted on Heroku?
use 'Content-Type' instead of 'Content-type'.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Simple_requests
https://fetch.spec.whatwg.org/#cors-safelisted-request-header
Explanation: when you use incorrect case then it is considered as a custom header and hence, a preflight request is sent in such cases. Now if OPTIONS request is implemented on server with correct cors spec then next POST will be sent, else it wont be sent and request will fail. More on this in above links.