请放心,如何在 POST 请求后从响应正文中提取生成的令牌并将其设置为标头 [英] Rest assured, how to extract generated token from response body after POST request and set it to header

查看:24
本文介绍了请放心,如何在 POST 请求后从响应正文中提取生成的令牌并将其设置为标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

执行任何请求,我需要使用 POST 请求与主体 {username:"somename", password:"somepass"} 执行身份验证,标头 Content-Type:application.json 给我一个带有生成令牌的响应,我需要将其粘贴为第二个标头,就像 Authorization:generated-tokenkjhsdkjfvjbwjbQ==进一步的请求.请你帮我一下好吗.

Performing any request, I need to perform Authentication with POST request with body {username:"somename", password:"somepass"}, header Content-Type:application.json which gives me a response with generated token, which I need to paste as a second header, smth like Authorization:generated-tokenkjhsdkjfvjbwjbQ== for further requests. Could you help me with it, please.

推荐答案

对我有用的变体:

String token = given()
                .contentType("application/json")
                .body(new User("someuser" , "123"))
                .when()
                .post(RestConfig.baseUrl+"/authentication-url")
                .then().extract().response().as(TokenResponse.class).getToken();

        given()
                .contentType("application/json")
                .header("Authorization", token)
                .get(RestConfig.baseUrl+"/some-path")
                .then()
                .statusCode(200)...

这篇关于请放心,如何在 POST 请求后从响应正文中提取生成的令牌并将其设置为标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆