在Swift中使用结构的POST请求

时间:2019-06-20 11:13:57

标签: ios swift networking

我使用结构发出POST请求。但是由于某种原因会发生错误。 API的响应是“找不到文件/注释。”

HTTP正文

['{% load i18n %}', '{% load staticfiles %}', '{% load storefront_urls %}', '{% load template_extra %}', '', '<!doctype html>', '<html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml">', '  <head>', '    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', '    <meta name="viewport" content="width=device-width, initial-scale=1.0" />', '    <title>Tax Invoice | {{ business_name }}</title>', '    <style>', '      @media print {', '        thead {display: table-header-group;}', '      }', '    </style>', '  </head>', '  <body style="position: relative;max-width:100%; margin: 0 auto; color: #001028;background: #fff; font-family: Arial, sans-serif; font-size: 15px; padding: 0 15px;" bgcolor="#fff">', '    <div class="signup-emailer">', '      <table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">', '        <tbody>', '          <tr>', '            <td align="center">', '              <table style="max-width:650px; width:100%; font-family: Arial, sans-serif; font-size: 12px; background-color: #fff;" align="center" border="0" cellpadding="0" cellspacing="0">', '                <tbody>', '                  <tr>', '                    <td width="15"></td>', '                    <td>', '                      <table width="100%" border="0" cellpadding="0" cellspacing="0">', '                        <tbody>', '                          <tr>', '                            <td width="15"></td>', '                            <td height="5"></td>', '                            <td width="15"></td>', '                          </tr>', '                          <tr>', '                            <td width="15"></td>', '                            <td valign="middle" align="center" style="padding:0 15px;">', '                              <a href="http://{{ domain_name }}" target="_blank" style="color: #000; text-decoration: none;">', '                                <h4 style="text-align: center;font-size: 18px;margin: 10px 0;font-weight: bold;">{{ business_name }}</h4>', '                              </a>', '                            </td>', '                            <td width="15"></td>', '                          </tr>', '                          <tr>', '                            <td width="15"></td>', '                            <td align="center" style="font-weight:bold; color:#000; font-size:16px; text-transform:uppercase;">', '                              Tax Invoice', '                            </td>', '                            <td width="15"></td>', '                          </tr>', '                          <tr>', '                            <td width="15"></td>', '                            <td height="10"></td>', '                            <td width="15"></td>', '                          </tr>', '                          <tr>', '                            <td width="15"></td>', '                            <td align="center" style="color: #636363; font-weight: 700;">Thanks for shopping!</td>', '                            <td width="15"></td>', '                          </tr>', '                          <tr>', '            

我成功创建并发送了HTTP BODY。

但是我没有看到API的响应。

CommentRequest结构

{
 rating: 10,
 writer: "coder",
 movie_id: "5a54c286e8a71d136fb5378e",
 contents: "GOOD!!!"
}

评论响应结构

struct CommentRequest: Codable {
    let comments: [Comment]

    enum CodingKeys: String, CodingKey {
        case comments
    }
}

struct Comment: Codable {
    let rating: Double //평점
    let writer: String
    let movieId: String
    let contents: String

    enum CodingKeys: String, CodingKey {
        case rating
        case writer
        case movieId = "movie_id"
        case contents
    }
}

使用结构发出POST请求

struct CommentResponse: Codable {
    let rating: Double //평점
    let timestamp: Double
    let writer: String
    let movieId: String
    let contents: String

    enum CodingKeys: String, CodingKey {
        case rating
        case timestamp
        case writer
        case movieId = "movie_id"
        case contents
    }
}

在发送注释请求后,如何使用CommentResponse结构获取API的响应?

0 个答案:

没有答案