Help us understand the problem. What is going on with this article?

NeustarでJSONをPOSTする

More than 1 year has passed since last update.

ハマったので備忘。
APIにJSON渡して、戻ってきたJSONを受け取ってまたゴニョゴニョしたいときなどに。

var client = test.openHttpClient();
client.setFollowRedirects(false);

test.beginTransaction();
test.beginStep("Step-001");

// Request
var url = 'https://example.com/api/getName';
var request = client.newPost(url);
request.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");

var json = {
   id:'hoge'
};
var strJson = JSON.stringify(json);
request.setRequestBody(strJson);

// Response
var response = request.execute();
var code = response.getStatusCode();
test.log(code);
var body = response.getBody();
test.log(body);

var obj = JSON.parse(body)
test.log(obj.name);

test.endStep();
test.endTransaction();
danishi
cloudpack
Amazon Web Services (AWS) の導入設計、環境構築、運用・保守をサポートするマネジドホスティングサービス
https://cloudpack.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away