gpt4 book ai didi

excel - VBA POST 格式问题

转载 作者:行者123 更新时间:2023-12-04 20:51:46 25 4
gpt4 key购买 nike

我正在尝试使用 VBA 发布到 API。我有过去使用过的代码(与其他 API 一起使用),其结构和格式都有效。但这次没有,罪魁祸首似乎是发送命令。因此,我在网上搜索并找到了一些有关如何格式化发送的不同示例,并且我已经尝试了所有这些示例,但它们都不起作用。

随附的代码示例包括我尝试格式化的几种方式。我已经注释掉了除一个之外的所有内容,但将其他内容留在了评论中以显示我尝试过的内容。当您查看代码时,请知道我已经尝试了所有可能的注释代码组合。

每次,我都会收到“没有上传文件或 URL 或 base 64 提供”的服务器响应。

但是,如果我将相同的变量放入 Postman 并从那里发送,它工作正常,所以我知道这不是 API 端的错误,这是我格式化 VBA 的方式。我已经检查了很多次数据,以确保没有错别字。

我将不胜感激任何建议。

    Sub macroPOST()
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Url = "https://api.ocr.space/parse/image"

objHTTP.Open "POST", Url, False

'objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "apikey", "helloworld"
objHTTP.setRequestHeader "Content-Type", "multipart/form-data"
'objHTTP.setRequestHeader "Content-Type", "image/png"

objHTTP.Send ("url=http://dl.a9t9.com/ocrbenchmark/eng.png&filetype=PNG&language=eng&isOverlayRequired=false&iscreatesearchablepdf=false&issearchablepdfhidetextlayer=false")
'objHTTP.Send ("url=http://dl.a9t9.com/ocrbenchmark/eng.png")
'objHTTP.Send ("url%3Dhttp%3A%2F%2Fdl%2Ea9t9%2Ecom%2Focrbenchmark%2Feng%2Epng")
BodyContent = "{" & Chr(34) & "url" & Chr(34) & ":" & Chr(34) & "http://dl.a9t9.com/ocrbenchmark/eng.png" & Chr(34) & "}"
'objHTTP.Send (BodyContent)

replyTXT = objHTTP.responseText

If objHTTP.Status = "200" Then 'success
MsgBox replyTXT
Else
MsgBox ("Problem")
End If
End Sub

最佳答案

你非常非常亲近。这只是将正确的 header 和请求正文的正确格式结合起来的问题。

它应该是这样的:

Option Explicit

Sub test()
Dim req As New WinHttpRequest 'add a reference to Microsoft WinHTTP Services version 5.1
Dim url As String
url = "https://api.ocr.space/parse/image"

With req
.Open "POST", url, False
.setRequestHeader "apikey", "helloworld"
.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
.send "url=http://dl.a9t9.com/ocrbenchmark/eng.png"
Debug.Print .responseText
Debug.Print .Status
End With
End Sub

JSON中的响应如下:

{"ParsedResults":[{"TextOverlay":{"Lines":[],"HasOverlay":false,"Message":"Text overlay is not provided as it is not requested"},"TextOrientation":"0","FileParseExitCode":1,"ParsedText":"ELLIOT\r\n29 Comments f Facebook Twitter G* Google O Pinterest in LinkedIn 6 Reddit\r\nLockheed's I-J-2S spy planes are famously difficult to launch and land. Their extremely poor field of\r\nvision requires a chase car on the ground that can keep up with them.\r\nThey use some powerful vehicles and now we learn that the Air Force has turned to the all-electric\r\nTesla Model S to launch its own spy planes.\r\nWhile it is still being used today, the Lockheed IJ-2S\r\nwas designed in 1950S. It has always been an\r\nextremely difficult plane to operate and Mythbusters\r\neven suggested that it could be \"the most difficul\r\nplane to fly\" in the world.\r\nThey need a whole ground team to put i in the air and\r\nback on the ground.\r\nThe US Air Force used a Camaro Z/28, which\r\nJalopnik featured in an article 4 years ago:\r\ nbaaa.fun\r\nmemes, quizzes, inspiration\r\nHave fun now!\r\nbaaa.fun\r\n","ErrorMessage":"","ErrorDetails":""}],"OCRExitCode":1,"IsErroredOnProcessing":false,"ProcessingTimeInMilliseconds":"621","SearchablePDFURL":"Searchable PDF not generated as it was not requested."}



现在,如果您需要在请求中包含更多可选参数,您可以像这样构造它的主体:
.send "url=http://dl.a9t9.com/ocrbenchmark/eng.png&language=eng&isOverlayRequired=false&filetype=jpg&iscreatesearchablepdf=false&issearchablepdfhidetextlayer=false"

评论中提到的括号问题在这种情况下不会产生影响,但它仍然是一个有效的观点。

关于excel - VBA POST 格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57703111/

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