0

我们正在尝试使用 Java 类中的 REST api 创建一个堆栈,但我们得到的响应代码为 400。如果您能告诉我们我们做错了什么,这将非常有帮助。

这是java类注意:请用类中正确的替换TOKEN_ID和TENANT_ID。

import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;

public class StackOrchestration {
private static final String TOKEN_ID = "xananananana";
private static final String TENANT_ID = "?????";

public static void createStack()
{
    HttpURLConnection connection = null;
    BufferedReader br = null;
    try {
        URL url = new URL("https://hkg.orchestration.api.rackspacecloud.com/v1/"+TENANT_ID+"/stacks");
        connection = (HttpURLConnection) url.openConnection();
        String line;
        StringBuffer jsonString = new StringBuffer();

        String payload = "{ \"stack_name\": \"Single-Server-Stack\", \"disable_rollback\": true, \"parameters\": {}, \"template\": \"heat_template_version: 2014-10-16\n \nresources:\n  compute_instance:  \n    type: \"OS::Nova::Server\"\n    properties:\n      flavor: 1 GB Performance\n      image: CentOS 5 (PV)\n      name: Single Server Stack\n       \noutputs:\n  public_ip:\n    description: public IP address of the deployed compute instance\n    value: { get_attr: [compute_instance, accessIPv4] }      \n\n\n\", \"timeout_mins\": 60 }";

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("X-Auth-Token", TOKEN_ID);
        connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        connection.setRequestProperty("Accept", "application/json");

        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        writer.write(payload);
        writer.close();
        br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        while ((line = br.readLine()) != null) {
            System.out.println("-- "+line);
        }
        br.close();
        connection.disconnect();
    } catch (Exception e) {
        InputStream error = ((HttpURLConnection) connection).getErrorStream();
        br = new BufferedReader(new InputStreamReader(error));

        String nachricht;
        String response = "";
        try {
            while ((nachricht = br.readLine()) != null){
                  response += nachricht;
            }
            System.out.println("+++ " + response);
        } catch (Exception ex) {
        }
        //e.printStackTrace();

        return;
    }
}

public static void main (String args[])
{
    try {
        createStack();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

Java 输出::

+++ 400 错误请求服务器无法满足请求,因为它格式错误或不正确。无效的控制字符位于:第 1 行第 129 列(字符 129)

当我们尝试使用'curl'创建堆栈时,我们能够使用我们作为有效负载传递给上述Java代码的相同模板属性。下面是 curl 的执行:

curl -i -X POST -H 'Accept: application/json' -H 'Content-Type: application/json'  -H  "X-Auth-Token: $OS_AUTH_TOKEN" -d \
'{
    "stack_name": "Single-Server-Stack",
    "disable_rollback": true,
    "parameters": {},
    "template": "heat_template_version: 2014-10-16\n \nresources:\n         compute_instance:  \n    type: \"OS::Nova::Server\"\n    properties:\n      flavor: 1 GB Performance\n      image: CentOS 5 (PV)\n      name: Single Server Stack\n       \noutputs:\n  public_ip:\n    description: public IP address of the deployed compute instance\n    value: { get_attr: [compute_instance, accessIPv4] }      \n\n\n", 
    "timeout_mins": 60
 }' \
 https://hkg.orchestration.api.rackspacecloud.com/v1/$OS_TENANT_ID/stacks     

卷曲输出::

HTTP/1.1 201 Created
Server: nginx/1.2.1
Date: Fri, 16 Jan 2015 11:27:59 GMT
Content-Type: application/json
Content-Length: 219
Connection: keep-alive
Location:         https://hkg.orchestration.api.rackspacecloud.com/v1/$OS_TENANT_ID/stacks/Single-Server-Stack/f1d5cc09-f4f8-4226-b5ce-62b9b26d111d
Via: 1.0 Repose (Repose/6.0.2)
{"stack": {"id": "f1d5cc09-f4f8-4226-b5ce-62b9b26d111d", "links":[{"href": "https://hkg.orchestration.api.rackspacecloud.com/v1/$OS_TENANT_ID/stacks/Single-Server-Stack/f1d5cc09-f4f8-4226-b5ce-62b9b26d111d", "rel": "self"}]}}

更新: 问题是因为没有添加转义字符。这是必须发送有效负载的方式:

String payload = "{ \"stack_name\": \"Single-Server-Stack\", \"disable_rollback\": true, \"parameters\": {}, \"template\": \"heat_template_version: 2014-10-16\\n \\nresources:\\n compute_instance: \\n type: \\\"OS::Nova::Server\\\"\\n properties:\\n flavor: 1 GB Performance\\n image: CentOS 5 (PV)\\n name: Single Server Stack\\n \\noutputs:\\n public_ip:\\n description: public IP address of the deployed compute instance\\n value: { get_attr: [compute_instance, accessIPv4] } \\n\\n\\n\", \"timeout_mins\": 60 }";

4

0 回答 0