gpt4 book ai didi

ios - 如何使用json上传图片?

转载 作者:搜寻专家 更新时间:2023-10-31 23:01:10 31 4
gpt4 key购买 nike

我正在尝试上传带有 json 数据的图像。我只是按照 the post 并实现了该机制。但是我得到了错误

{ status code: 400, headers {
"Cache-Control" = "no-cache, no-store, max-age=0, must-revalidate";
Connection = close;
"Content-Language" = en;
"Content-Length" = 1033;
"Content-Type" = "text/html;charset=utf-8";
Date = "Wed, 27 Jan 2016 10:44:34 GMT";
Expires = 0;
Pragma = "no-cache";
Server = "Apache-Coyote/1.1";
"X-Content-Type-Options" = nosniff;
"X-XSS-Protection" = "1; mode=block";
} }`

下面是完整的 HTTP 请求

Content-Type: multipart/form-data;boundary=Boundary_123456789
Authorization: Basic bihdwbcIUkbcdwjnoNOn
User-Agent: Jersey/2.21.1 (HttpUrlConnection 1.8.0_45)
MIME-Version: 1.0
Host: localhost:8080
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 3526

--Boundary_123456789
Content-Type: application/json
Content-Disposition: form-data; name="userDTO"

{"id":"id","name":"name","age":23}
--Boundary_123456789
Content-Type: image/png
Content-Disposition: form-data; filename="sample-image2.png"; modification-date="Fri, 22 Jan 2016 04:56:48 GMT"; size=3308; name="file"

‰PNG

<Binary data>

--Boundary_123456789—

下面是我的实现

func addUser(completion: (message: String?, error: String?) -> Void) -> NSURLSessionDataTask {
// create the request
let request = createRequest()

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
print(response) // 400 Error is printed here
}
task.resume()

return task
}
func createRequest () -> NSURLRequest {
let param = [
"id": "id",
"name": "name",
"age": 23] // build your dictionary however appropriate

let boundary = generateBoundaryString()

let url = NSURL(string: SERVERURL)!
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.addValue("Basic \(base64LoginString())", forHTTPHeaderField: "Authorization")
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")

let path1 = NSBundle.mainBundle().pathForResource("userImage", ofType: "png") as String!
request.HTTPBody = createBodyWithParameters(param, paths: [path1], boundary: boundary)

return request
}

func createBodyWithParameters(json: [String:AnyObject], paths: [String]?, boundary: String) -> NSData {
let body = NSMutableData()
let key = "userDTO"

body.appendString("--\(boundary)\r\n")
body.appendString("Content-Type: application/json")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
var requestBody = NSData()

do {
requestBody = try NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions(rawValue:0))
} catch (let e) {
print(e)
}
body.appendData(requestBody)
//body.appendString("\(json)\r\n")

if paths != nil {
for path in paths! {
let url = NSURL(fileURLWithPath: path)
let data = NSData(contentsOfURL: url)!
let mimetype = mimeTypeForPath(path)
let fileName = "sample-image23.png"
let date = "Fri, 22 Jan 2016 04:56:48 GMT"
let name = "file"

body.appendString("--\(boundary)\r\n")
body.appendString("Content-Type: \(mimetype)\r\n\r\n")
body.appendString("Content-Disposition: form-data; filename=\"\(fileName)\"; modification-date=\"\(date)\"; size=3308; name=\"\(name)\"\r\n")
body.appendData(data)
body.appendString("\r\n")
}
}

body.appendString("--\(boundary)--\r\n")
return body
}

func generateBoundaryString() -> String {
return "Boundary-\(NSUUID().UUIDString)"
}

func mimeTypeForPath(path: String) -> String {
let url = NSURL(fileURLWithPath: path)
let pathExtension = url.pathExtension

if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension! as NSString, nil)?.takeRetainedValue() {
if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
return mimetype as String
}
}
return "application/octet-stream";
}

还有扩展

extension NSMutableData {

func appendString(string: String) {
let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
appendData(data!)
}
}

我想我在 json 格式上犯了一些错误。有人可以帮忙吗?谢谢!

最佳答案

看来,上传图片不是你的问题——就我所见,它成功了。你应该意识到,上传图像与 JSON 无关。相反,以您期望的格式 (JSON) 获得响应可能正是您所寻求的。因此,如果您需要获得正文为 JSON 的响应,您应该通过设置适当的 Accept header 来明确说明这一点。例如:

接受:application/json

在代码中:

request.setValue("application/json", forHTTPHeaderField: "Accept")

当你得到响应时,你还应该首先检查响应的状态代码,然后是 Content-Type header (分别是响应的 MIMEType 属性),它应该符合您的期望:application/json

如果内容类型不是您所期望的,您可以选择尝试额外的“响应序列化程序”——每个都适合解析其他内容类型,例如text/plain等,随你喜欢。

编辑:

服务器响应提示,多部分请求的第二部分格式错误。看看它是如何组成的:

        body.appendString("--\(boundary)\r\n")
body.appendString("Content-Type: \(mimetype)\r\n\r\n")
body.appendString("Content-Disposition: form-data; filename='sample-image23.png'; modification-date='Fri, 22 Jan 2016 04:56:48 GMT'; size=3308; name='file'\r\n")
body.appendData(data)
body.appendString("\r\n")

现在,仔细观察,我们可以看到第二个标题 Content-Type 将用 two CRLF 分隔 - 但还有另一个标题下列的。 header 应仅用一个 CRLF 分隔。

然后,最后一个 header 必须用 两个 CRLF 分隔。

建议的修复:

        body.appendString("--\(boundary)\r\n")
body.appendString("Content-Disposition: form-data; filename='sample-image23.png'; modification-date='Fri, 22 Jan 2016 04:56:48 GMT'; size=3308; name='file'\r\n")
body.appendString("Content-Type: \(mimetype)\r\n\r\n")
body.appendData(data)
body.appendString("\r\n")

(编辑:删除了不正确的描述)

编辑2:

这些行中还缺少一个 CRLF,在这一行之上:

let key = "userDTO"

body.appendString("--\(boundary)\r\n")
body.appendString("Content-Type: application/json")
body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")

你看到问题了吗? Content-Type 没有尾随 CRLF!

另请参阅:NSURLRequest Upload Multiple Files

关于ios - 如何使用json上传图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34992034/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com