使用REST API

时间:2016-10-18 11:19:47

标签: rest api http unirest

使用REST API(https://qwintry.com/ru/api-docs),尝试使用make方法注册新用户。 有关注册用户请求的API文档的一部分:

Login
Request example:
<?php
    define('site_url', 'qwintry.com');
    $url =  'https://' . site_url.'/api-rest/v2/user/login';
    $data = array ( 
            'email' => 'op@b.c',
            'password' => '123',
            'key' => '9e4fddbb3adc4c67f74bb2b7757cebf9',
     );
    $data_string = http_build_query($data);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,  $data_string);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $response = curl_exec($ch);
    curl_close($ch);
    print_r($response);
Parameters:
email — E-mail of user
password — Password
key — Unique key of request

我的方法loginUser的实现:

@Service
public class LogisticServiceImpl implements LogisticService {
    private final String BASE_URL = "http://www.qwintry.com/api-rest/v2";

    @Override
    public String userLogin(String email, String password) throws Exception {
        String url = BASE_URL + "/user/login";
        Map<String, Object> params = new HashMap<>();
        params.put("email", email);
        params.put("password", password);
        HttpResponse<String> jsonResponse = Unirest.post(url).fields(params).asString();

        return jsonResponse.getBody();
    }

永久获得此类回复:

  

301永久移动

301永久移动

  
nginx

预期的Json格式。 我做错了什么?

1 个答案:

答案 0 :(得分:0)

在文档中发现错误,它不是POST而是GET请求,现在一切正常