Giter VIP home page Giter VIP logo

Comments (2)

mrsarm avatar mrsarm commented on September 25, 2024

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.

deepakkbc avatar deepakkbc commented on September 25, 2024

Thank you for your reply.I was missing fullResponse: true
, now it works. thanks

from reqclient.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.