Comments (2)
Hi @deepakkbc,
I'm not sure about the issue, are you aware that the object resp
in the let resp = client.po...
statement is a Promise object? You need to get the response once the promise is resolved, check the README of the project with some examples of how to get the response from a .then(...
chaining, or using async/await, but in both cases, dealing with promise is not as easy as looks like, you should take some extra reads to undestand well how promises and async programming works.
But if you are aware of that and the example provided is just an extract of the whole code, yes, it wont give you the statusCode
in that way (but there is a way below), but default reqclient only returns the response body, and if it's a JSON response, it will unserialize it to give you the response as a JS object to be easily managed.
The library do this because most of the time you don't need the status code, becuase if something fails and the status code is not between 200-299, it will return promise that it would rejects, so you will put your code to treat failures in the catch()
block, or ...catch {}...
if you are using async / await syntax.
That said, some time is need to catch the status code to get a more granular idea of what kind of success or failure we are dealing with, in these cases, you need to pass the option fullResponse: true
to get the all response object. You can set this option in the client creation if you want this behaviour for all the requests in your project, in your example it would be like this:
var client = new RequestClient({
baseUrl:"http://19.160.168.50:3000/api",
debugRequest:false, debugResponse:true,
fullResponse: true
});
Or you can pass to only some requests and let the default behaviour for the rest of the requests:
let resp = client.post("User", jsData, {fullResponse: true, headers: {"Content-Type":"application/json", "Accept":"application/json"}});
Then in both cases you can check the status code and get the response body like this (example extracted from the README page):
resp.then(httpResponse => {
if (httpResponse.statusCode == 201) {
// Registry created, do something with httpResponse.body ...
} else if (httpResponse.statusCode == 200) {
// Registry updated, do something with httpResponse.body ...
} else {
// Do something
}
});
from reqclient.
Thank you for your reply.I was missing fullResponse: true
, now it works. thanks
from reqclient.
Related Issues (10)
- cURL debug with body strings must be escaped when single quote "'" is present in a string
- Add support to get the full response HOT 1
- Improve the query encoding HOT 1
- Add in the config obj a timeout property just for file uploads HOT 1
- OAuth2 login with unauthorized error with JSON response are propagated as a string message HOT 1
- Error array parameters in "query" object parsed as unique param in URIs HOT 1
- Implement L2 cache
- Implement compression and keep alive mechanism to boost performance HOT 1
- invalid_request, can't post parameters HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from reqclient.