简体   繁体   中英

azure 400 bad request hits from postman

When I am executing below peace of code in java environment it is executing pretty well but If I am executing same with node js or postman it is throwing an error

I am trying to hit the url in postman generated in url3 variable is returning 400 bad request.

request headers also passed along with the url.

but same url getting succeed in java environment.

private HttpResponse accessTokenStep2(HttpResponse response, Environment environment, String email, String password) throws Exception {
    if(response.getStatusLine().getStatusCode() == 200) {
        String postResponse = EntityUtils.toString(response.getEntity());
        this.transId = getConnectionString(postResponse.toString(), "transId\":\"", "\",\"pageMode\"", 10);
        this.csrfId = getConnectionString(postResponse.toString(), "\"csrf\":\"", "\",\"transId\"", 8);
        String app = environment.getAppUrl();
        String tenant = environment.getTenantId();
        String policy = environment.getPolicy();
        String url2 = "https://" + app + "/" + tenant + "/" + policy + "/SelfAsserted?tx=" + this.transId + "&p=" + policy;
        this.httpPost = new HttpPost(url2);
        List<NameValuePair> nameValuePairs = new ArrayList(3);
        nameValuePairs.add(new BasicNameValuePair("request_type", "RESPONSE"));
        nameValuePairs.add(new BasicNameValuePair("signInName", email));
        nameValuePairs.add(new BasicNameValuePair("password", password));
        nameValuePairs.add(new BasicNameValuePair("g-recaptcha-response-toms", "03AEHxwuy8mq_8jbEWU--XHGea2dF-p7F-oUmZhAgpr1Xt7XyyrzBQpJZJilwhJZPG3D-K_HldpTEEl_2Xi51xm4ICCg6-w66XfR5nE1-Cj39ytH6QZiY2yJUTy-1CJZ6qNfJBsO7EG2Kba7cSGhQgetkhAxJOBtI-FJt5WlRdjP6UHgId11rSVhSCS2kphf1Z-BO62Fu2Jm6jWxnbTiVceEVXuQTOEibtQnQ-n2ckOLI-i0GI1tFBAVi6Lu6NZkQII7m_00ZyJtSoik-1LCNNQ8GXmOxFnnVTnvm-vr3mk_RmNaLS9CT6eGlo0CfpK3DiVTLEArQDTuxY"));
        this.httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        this.httpPost.setHeader("User-Agent", this.userAgent);
        this.httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        this.httpPost.setHeader("Accept", "application/json, text/javascript, **/*//*; q=0.01");
        this.httpPost.setHeader("X-Requested-With", "XMLHttpRequest");
        this.httpPost.setHeader("X-CSRF-TOKEN", this.csrfId);
        response = this.httpClient.execute(this.httpPost);
        return response;
    } else {
        throw new Exception("Unable to get access token step 2");
    }
}

please find the below node function which is calling the same api

    accessTokenStep2 : (obj,env,email,password) => {

    let transId = obj.transid;
    let csrfId = obj.csrfId;
    let app = env.getAppUrl();
    let tenant = env.getTenantId();



    let policy = env.getPolicy();
    let url2 = ("https://" + app + "/" + tenant + "/" + policy + "/SelfAsserted");


    var options = {
        request_type   : "RESPONSE",
        signInName : "*********",
        password : "*****",
        "g-recaptcha-response-toms" : "03AEHxwuy8mq_8jbEWU--XHGea2dF-p7F-oUmZhAgpr1Xt7XyyrzBQpJZJilwhJZPG3D-K_HldpTEEl_2Xi51xm4ICCg6-w66XfR5nE1-Cj39ytH6QZiY2yJUTy-1CJZ6qNfJBsO7EG2Kba7cSGhQgetkhAxJOBtI-FJt5WlRdjP6UHgId11rSVhSCS2kphf1Z-BO62Fu2Jm6jWxnbTiVceEVXuQTOEibtQnQ-n2ckOLI-i0GI1tFBAVi6Lu6NZkQII7m_00ZyJtSoik-1LCNNQ8GXmOxFnnVTnvm-vr3mk_RmNaLS9CT6eGlo0CfpK3DiVTLEArQDTuxY",
        tx : transId,
        p : policy,

    };


 return new Promise( (resolve, reject) => {

     request({
      headers: [{
          'User-Agent'          :  'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0',
          'Content-Type'        : 'application/x-www-form-urlencoded',
          'Accept'              : 'application/json, text/javascript, **/*//*; q=0.01',
          'X-Requested-With'    : 'XMLHttpRequest',
          'X-CSRF-TOKEN'        : csrfId
        }],
        url: url2,
        method: "POST",
        form   : options,
        json: true          
      }, function(error, response, body){

         // console.log(error);
          //console.log(body);
         (response.statusCode == 200 ? resolve(body) : resolve(error));
    });

 });

}

headers is an object rather than an array, see Custom HTTP Headers .

Try:

headers: {
    'User-Agent'          :  'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0',
    'Content-Type'        : 'application/x-www-form-urlencoded',
    'Accept'              : 'application/json, text/javascript, **/*//*; q=0.01',
    'X-Requested-With'    : 'XMLHttpRequest',
    'X-CSRF-TOKEN'        : csrfId
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM