ranm8 / requestify

Simplifies node HTTP request making.
http://ranm8.github.io/requestify
MIT License
223 stars 55 forks source link

Can't asign content-type on POST #58

Open megamauricio009 opened 7 years ago

megamauricio009 commented 7 years ago

Hi guys i'm trying to get a token from a service but it said's "error":"invalid_request","error_description":"Missing grant type i was searching about this error and the solution is changing the content-type to "application/x-www-form-urlencoded" cause it can be sended by JSON. but it doesn't work, my code is this

` var json = JSON.parse(JSON.stringify(jsonResp))

var body = decryptBody(req.body); body = body.replace(/\0/g, '') body = JSON.parse(body) var auth = {grant_type:"password", username: "pass" , password: "usr" } if(!isValidProtocol(body)){ //console.log(body) json.error = true, json.desc = 'invalid protocol' res.statusCode = 202; res.json(encryptBody(json)); }

requestify.request( "http://localhost:7070" + '/oauth/token',{ method: "POST", headers: {'Content-Type': 'application/x-www-form-urlencoded'}, dataType: 'form-url-encoded', auth: auth }) .then(function(response) { json.data = response.getBody(); res.json(encryptBody(json)); }).catch(function(err){ console.log('ERROR:') console.log(err) //console.log(body.data) console.log(auth) json.error = true, json.desc = 'invalid login params' res.statusCode = 202; res.json(encryptBody(json)); });`

and the result

ERROR: Response { code: 400, headers: { 'x-content-type-options': 'nosniff', 'x-xss-protection': '1; mode=block', 'cache-control': 'no-cache, no-store, max-age=0, must-revalidate', pragma: 'no-cache', expires: '0', 'x-frame-options': 'DENY', 'content-type': 'application/json;charset=UTF-8', 'transfer-encoding': 'chunked', date: 'Tue, 20 Jun 2017 19:31:43 GMT', connection: 'close' }, body: '{"error":"invalid_request","error_description":"Missing grant type"}' } Waitng for the answer, THANK YOU..