【问题标题】:PUT request giving 400 Bad Request ErrorPUT 请求给出 400 错误请求错误
【发布时间】:2018-06-19 16:11:59
【问题描述】:

我正在使用Google Contact API 实现一个联系人应用程序。 现在我正在尝试通过以下格式发送 put 请求来更新联系人

PUT /m8/feeds/contacts/default/full/{contactId}
If-Match: {lastKnownEtag}
GData-Version: 3.0
Content-Type: application/atom+xml

我将 XML 作为字符串作为请求正文发送。 这是我的 xmlString(请求正文

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:gd="http://schemas.google.com/g/2005" gd:etag="*">
<id>http://www.google.com/m8/feeds/contacts/default/base/1785xxxx</id>
<catagory scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
<gd:name>
<gd:fullname>abc</gd:fullname></gd:name>
<gd:email address="abc@gmail.com" displayName="abc" primary="true" rel="http://schemas.google.com/g/2005#work"/>
<content type="text">Notes</content>
<gd:phoneNumber primary="true" rel="http://schemas.google.com/g/2005#other">9090xxxxxx</gd:phoneNumber>
</entry>

我编写了以下代码来发送 PUT 请求以更新联系人。

    String getUrl = "https://www.google.com/m8/feeds/contacts/default/full/"+contactID+"?oauth_token=" + accessToken;         
    URL url = new URL(getUrl);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();        
    con.setDoOutput(true);          
    con.setRequestMethod("PUT");
    con.setRequestProperty("Content-Type", "application/atom+xml" );
    con.setRequestProperty("GData-Version","3.0"); 
    con.setRequestProperty("IF-MATCH", "*");
    OutputStreamWriter output = new OutputStreamWriter(con.getOutputStream());      
    output.write(xmlString);   
    // xmlString is the body of the request
    output.flush();
    output.close();
    System.out.println(con.getResponseCode());

当我尝试在 OAuth 2.0 Playground 中发送请求时,联系人已成功更新。 但是当我尝试运行上述程序时,我得到了

400 错误请求错误

我不知道我哪里错了。任何帮助将不胜感激!

【问题讨论】:

  • 那是什么要求。请使用代理来显示它是什么。我敢打赌 xmlString 是空的或无效的
  • @Daij-Djan 我尝试在 Google Playground 中发送相同的 xmlString 并获得 200 OK 的响应。所以xmlString没有问题。
  • @Daij-Djan 我也添加了我的 xmlString 。请告诉我我的字符串是否无效。
  • 请求正文在哪里?
  • @noogui xmlString 是请求的正文。我在帖子中提到过,我也给出了 xmlString。

标签: java put google-contacts-api bad-request


【解决方案1】:

我终于找到了错误的地方。

我的 xmlString 无效。 &lt;entry&gt; 标记需要另一个命名空间 xmlns="http://www.w3.org/2005/Atom",这在 https://developers.google.com/contacts/v3Google Contact API)中未提及。 这就是我收到 400 Bad request 错误的原因。

有效的 xmlString

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:gd="http://schemas.google.com/g/2005" gd:etag="*" xmlns="http://www.w3.org/2005/Atom">
    <id>http://www.google.com/m8/feeds/contacts/default/base/1785xxxx </id>
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" />
    <gd:name>
        <gd:fullName>name</gd:fullName>
    </gd:name>
    <gd:email address="juli@gmail.com" displayName="juli" primary="true" rel="http://schemas.google.com/g/2005#work" />
    <content type="text">Notes</content>
    <gd:phoneNumber primary="true" rel="http://schemas.google.com/g/2005#other">9090xxxxxx</gd:phoneNumber>
</entry>

【讨论】:

    猜你喜欢
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2018-06-14
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 2015-07-05
    相关资源
    最近更新 更多