简体   繁体   English

发送带有apollo-link-rest的纯文本请求正文

[英]Send plain text request body with apollo-link-rest

I am trying to send a POST request to an endpoint that takes a application/x-www-form-urlencoded Content-Type and a plain text string for the form data with the apollo-link-rest module and am having the hardest time. 我正在尝试向具有apollo-link-rest模块的form数据的application/x-www-form-urlencoded Content-Type和纯文本字符串的端点发送POST请求,并且时间最艰难。

In cURL form the request I want to make looks like this: 在cURL形式中,我要发出的请求如下所示:

curl -X POST http://tld.com/search -d include_all=My%20Search%20Term

I have wrapped my main component in the graphql HOC from react-apollo like this. 我已经像这样将我的主要组件从react-apollo包裹在graphql HOC中。

export default graphql(gql`
  mutation productSearch($input: string) {
    search(input: $input) @rest(
      path: "/search",
      method: "post",
      endpoint: "search",
      bodySerializer: "search"
    ) {
      total
    }
  }
`,
{
  props: ({ mutate }) => ({
    runSearch: (text: string) => {
      if (mutate) {
        mutate({
          variables: {
            input: `include_all=${encodeURIComponent(text)}`,
          },
        });
      }
    },
  }),
})(SearchResults);

The search bodySerializer referenced in the query looks like this. 查询中引用的搜索bodySerializer如下所示。

const searchSerializer = (data: any, headers: Headers) => {
  headers.set('Content-Type', 'application/x-www-form-urlencoded');
  return { body: data, headers };
};

And then have called the runSearch function like this in my component. 然后在我的组件中这样调用runSearch函数。

async componentDidMount() {
  try {
    const result = await this.props.runSearch(this.props.searchText);
  } catch (error) {
    // report error & show message
  }
}

Now I realize I'm not doing anything with the results but there seems to be an unhandled promise rejection (that's what React Native is telling me with a yellow box warning) when running the search code. 现在我意识到我对结果不做任何事情,但是在运行搜索代码时似乎有未处理的承诺拒绝(这是React Native用黄色框警告告诉我的)。 I'm examining the request with Reactotron as well and the request looks good, but it fails still. 我也在用Reactotron检查该请求,该请求看起来不错,但仍然失败。 I'm wondering if I'm missing something with how I'm configuring apollo-link-rest or if there's a better way I can examine requests made from the Apollo client. 我想知道我是否在配置apollo-link-rest缺少什么,或者是否有更好的方法可以检查从Apollo客户端发出的请求。

Any help here would be much appreciated. 在这里的任何帮助将不胜感激。 Thanks! 谢谢!

So it turns out that I had everything setup correctly above. 事实证明,我在上面已正确设置了所有设置。 Instead of it being an issue with react-apollo it was an issue with my Info.plist file. 我的Info.plist文件不是问题,而是react-apollo的问题。 I hadn't enabled the ability of the iOS app to make HTTP requests. 我尚未启用iOS应用发出HTTP请求的功能。 It was only allowing HTTPS. 它只允许HTTPS。 I fixed it with this entry in my Info.plist. 我使用Info.plist中的此项修复了该问题。

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM