I try to call a web service using the package UTL_HTTP, it works but I have an issue with french accents( 'é' and 'è') , the UTF-8 is not working .
CONTENT := '{
"metier": {
"REF_CONTRAT": "'||ref_contrat||'",
"ID_HISTO": "'||id_histo||'",
"ID_OBJ_DECLENCHEUR": "'||ID_OBJ_DECLENCHEUR||'",
"TYPE_OBJ_DECLENCHEUR": "'||type_obj_declencheur||'",
"ID_SCENARIO_INSTANCIE": "'||ID_SCENARIO_INSTANCIE||'",
"ID_SCENARIO": "'||id_scenario||'",
"ID_SMS": "'||id_sms||'",
"REPONSE_RECUE": "'||reponse_recue||'",
"ACTION": "'||ACTION||'",
"TYPE_ACTION": "'||TYPE_ACTION||'"
}
}';
-- V_URL:=UTL_URL.ESCAPE(V_URL,FALSE,'UTF-8');
UTL_HTTP.SET_BODY_CHARSET('UTF-8');
REQ := UTL_HTTP.BEGIN_REQUEST(V_URL, V_METHODE,' HTTP/1.1');
UTL_HTTP.SET_HEADER(REQ, 'user-agent', 'mozilla/4.0');
-- UTL_HTTP.SET_HEADER(REQ, 'content-type', V_CONTENT_TYPE);
UTL_HTTP.SET_HEADER(REQ, 'content-type','application/json; charset="UTF-8"');
utl_http.set_header(req, 'Content-Length', lengthB(content));
UTL_HTTP.SET_HEADER(REQ,'Authorization',V_AUTHORIZATION);
UTL_HTTP.WRITE_TEXT(REQ, CONTENT);
/* UTL_HTTP.WRITE_RAW (R => REQ,
data => UTL_RAW.CAST_TO_RAW(CONTENT)); */
I have error 500 as a response.
any idea please ? I tried all what I found on the internet but it's not working
The problem was that length doesn't give the right length when the question contains combined characters ; é was counted as 1 byte instead of 2 so it worked when I converted the request to AL32UTF8 : length(Convert(CONTENT,'AL32UTF8'))
Related
this is my function:
func downloadDoc(c *gin.Context) {
var fileToSearch service.ApDocumentsMedia
if err := c.BindJSON(&fileToSearch); err != nil {
c.AbortWithStatusJSON(http.StatusUnprocessableEntity, "is not binding!")
return
}
var file service.ApDocumentsMedia
if err := db.Where("uuid = ?", fileToSearch.Uuid).First(&file); err.RowsAffected <= 0 {
c.IndentedJSON(http.StatusNotFound, "Document not founded!")
return
}
c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", "attachment; filename="+strconv.Quote(file.Path))
c.Header("Content-Type", "application/octet-stream; charset=utf-8")
c.File(file.Path)
c.IndentedJSON(http.StatusOK, "File inviato")
}
but then when I go to call the function (using the frontend) it gives me this error:
http: panic serving 127.0.0.1:50138: http: wrote more than the declared Content-Length
goroutine 24 [running]:
net/http.(*conn).serve.func1()
/home/stage01/sdk/go1.18.3/src/net/http/server.go:1825 +0xbf
panic({0xac00e0, 0xc000020ab0})
/home/stage01/sdk/go1.18.3/src/runtime/panic.go:844 +0x258
github.com/gin-gonic/gin.(*Context).Render(0xc0000b2900, 0xc8, {0xc8d318, 0xc000245950})
/home/stage01/go/pkg/mod/github.com/gin-gonic/gin#v1.8.1/context.go:911 +0x112
github.com/gin-gonic/gin.(*Context).IndentedJSON(...)
/home/stage01/go/pkg/mod/github.com/gin-gonic/gin#v1.8.1/context.go:928
main.downloadDoc(0xc0000b2900)
/home/stage01/Scrivania/minia_git/docmanagement-alexperrucci/api/main.go:74 +0x36d
github.com/gin-gonic/gin.(*Context).Next(...)
/home/stage01/go/pkg/mod/github.com/gin-gonic/gin#v1.8.1/context.go:173
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc000449520, 0xc0000b2900)
/home/stage01/go/pkg/mod/github.com/gin-gonic/gin#v1.8.1/gin.go:616 +0x671
github.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc000449520, {0xc8f430?, 0xc0004ee620}, 0xc0000b2700)
/home/stage01/go/pkg/mod/github.com/gin-gonic/gin#v1.8.1/gin.go:572 +0x1dd
net/http.serverHandler.ServeHTTP({0xc8c968?}, {0xc8f430, 0xc0004ee620}, 0xc0000b2700)
/home/stage01/sdk/go1.18.3/src/net/http/server.go:2916 +0x43b
net/http.(*conn).serve(0xc00054b360, {0xc8ff38, 0xc00028f290})
/home/stage01/sdk/go1.18.3/src/net/http/server.go:1966 +0x5d7
created by net/http.(*Server).Serve
/home/stage01/sdk/go1.18.3/src/net/http/server.go:3071 +0x4db
This problem is given to me when once I click on the button that makes the post call (with the uuid of the file) it gives me this error and I don't understand why, would anyone know how to help me?
I am struggling to know how to access the response to an Indy POST request. I post the data either as JSON or paramstring. My code when using JSON is as follows.
params := TStringList.Create;
try
params.Text :=
'{'
+ format ('"client_secret":"%s",', [FilesFrm.ClientSecret])
+ format ('"client_id":"%s",', [FilesFrm.ClientId])
+ '"grant_type":"authorization_code",'
+ '"redirect_uri":"http://localhost:8080",'
+ format ('"code":"%s"', [fCode])
+ '}';
idLogFile1.Active := true;
// Make sure it uses HTTP 1.1, not 1.0
IdHTTP1.HTTPOptions := IdHTTP1.HTTPOptions + [hoKeepOrigProtocol];
IdHTTP1.Request.ContentType := 'application/json';
IdHttp1.Request.Accept := 'application/vnd.hmrc.1.0+json';
try
result := IdHTTP1.Post (
'https://test-api.service.hmrc.gov.uk/oauth/token',
params);
except
on E: Exception do
memo1.lines.add (E.ClassName + ': ' + E.message);
end;
memo1.Lines.add (result);
memo1.Lines.add (idHTTP1.ResponseText);
finally
params.free;
end;
The result of printing out result and RepsonseText is just
EIdHTTPProtocolException: HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
However, because I have a TidLogFile component attached to the TidHTTP, I can see what actually arrives, which is the following.
Recv 2/1/2019 7:56:07 AM: HTTP/1.1 400 Bad Request<EOL>
Content-Type: application/json<EOL>
Content-Length: 72<EOL>
Cache-Control: no-cache,no-store,etc, etc...
; Secure; HTTPOnly<EOL><EOL>
{"error":"invalid_request","error_description":"grant_type is required"}
Aside from the fact that grant_type appears to be in the original request data, I would like to be able to access the JSON response at the end, since "grant_type_is_required" is much more helpful than "Bad request", but I cannot find where it is.
I have subsequently found Response.ContentLength, which contains the value 72, and Response.ContentStream, which should in theory contain the 72 bytes of data, but produces access violations when I try to extract the data.
len := idHTTP1.Response.ContentLength;
memo1.Lines.Add(format ('Length = %d', [len]));
if assigned (idHTTP1.Response.ContentStream) then
begin
//idHTTP1.Response.ContentStream.Position := 0;
result := ReadStringFromStream (idHTTP1.Response.ContentStream, len);
end;
memo1.lines.add (result);
In addition to mjn42's answer, which is technically correct, TIdHTTP also has optional hoNoProtocolErrorException and hoWantProtocolErrorContent flags in its HTTPOptions property, which you can enable to avoid the EIdHTTPProtocolException being raised and to populate your result variable with error data, respectively:
params := TStringStream.Create(
'{'
+ format ('"client_secret":"%s",', [FilesFrm.ClientSecret])
+ format ('"client_id":"%s",', [FilesFrm.ClientId])
+ '"grant_type":"authorization_code",'
+ '"redirect_uri":"http://localhost:8080",'
+ format ('"code":"%s"', [fCode])
+ '}',
TEncoding.UTF8);
try
IdLogFile1.Active := true;
// Make sure it uses HTTP 1.1, not 1.0,
// and disable EIdHTTPProtocolException on errors
IdHTTP1.ProtocolVersion := pv1_1;
IdHTTP1.HTTPOptions := IdHTTP1.HTTPOptions + [hoKeepOrigProtocol, hoNoProtocolErrorException, hoWantProtocolErrorContent];
IdHTTP1.Request.ContentType := 'application/json';
IdHTTP1.Request.Accept := 'application/vnd.hmrc.1.0+json';
try
result := IdHTTP1.Post('https://test-api.service.hmrc.gov.uk/oauth/token', params);
except
on E: Exception do begin
Memo1.Lines.Add(E.ClassName + ': ' + E.message);
raise;
end;
end;
Memo1.Lines.Add(result);
finally
params.Free;
end;
Here is an example which shows how the HTTP body can be accessed.
The body can be accessed if you catch the EIdHTTPProtocolException exception.
on E: EIdHTTPProtocolException do
begin
WriteLn(E.Message);
WriteLn(E.ErrorMessage);
end;
Full example code:
program JSONPostExample;
{$APPTYPE CONSOLE}
uses
IdHTTP, IdGlobal, SysUtils, Classes;
var
HTTP: TIdHTTP;
RequestBody: TStream;
ResponseBody: string;
begin
HTTP := TIdHTTP.Create;
try
try
RequestBody := TStringStream.Create('{"日本語":42}',
TEncoding.UTF8);
try
HTTP.Request.Accept := 'application/json';
HTTP.Request.ContentType := 'application/json';
ResponseBody := HTTP.Post('https://httpbin.org/post',
RequestBody);
WriteLn(ResponseBody);
WriteLn(HTTP.ResponseText);
finally
RequestBody.Free;
end;
except
on E: EIdHTTPProtocolException do
begin
WriteLn(E.Message);
WriteLn(E.ErrorMessage);
end;
on E: Exception do
begin
WriteLn(E.Message);
end;
end;
finally
HTTP.Free;
end;
ReadLn;
ReportMemoryLeaksOnShutdown := True;
end.
Note that you must not use a TStringList for the POST body. That version of TIdHTTP.Post() formats the data according to the application/x-www-form-urlencoded media type, which is not appropriate for JSON and will corrupt it.
I am trying to read from a tcp connection which contains HTTP/2 data. Below is the code for reading HEADERS frame -
framer := http2.NewFramer(conn, conn)
frame, _ := framer.ReadFrame()
fmt.Printf("fh type: %s\n", frame.Header().Type)
fmt.Printf("fh type: %d\n", frame.Header().Type)
fmt.Printf("fh flag: %d\n", frame.Header().Flags)
fmt.Printf("fh length: %d\n", frame.Header().Length)
fmt.Printf("fh streamid: %d\n", frame.Header().StreamID)
headersframe := (frame1.(*http2.HeadersFrame))
fmt.Printf("stream ended? %v\n", headersframe.StreamEnded())
fmt.Printf("block fragment: %x\n", headersframe.HeaderBlockFragment())
I send request using curl as -
curl -v https://127.0.0.1:8000/ -k --http2
This is the output I get (after reading connection preface and SETTINGS), if I read from the conn using above code -
fh type: HEADERS
fh type: 1
fh flag: 5
fh length: 30
fh streamid: 1
stream ended? true
block fragment: 828487418a089d5c0b8170dc6c4d8b7a8825b650c3abb6f2e053032a2f2a
I understand the ouput, except the block fragment part and how to decode it into ascii string? I want to know the protocol/method/url path information.
The "header block fragment" is encoded using HPACK.
Go has an implementation to encode and decode HPACK, so you don't have to write your own.
You can find here an example of using both the encoder and decoder Go API.
I figured it out using Go hpack library (https://godoc.org/golang.org/x/net/http2/hpack) -
decoder := hpack.NewDecoder(2048, nil)
hf, _ := decoder.DecodeFull(headersframe.HeaderBlockFragment())
for _, h := range hf {
fmt.Printf("%s\n", h.Name + ":" + h.Value)
}
This prints -
:method:GET
:path:/
:scheme:https
:authority:127.0.0.1:5252
user-agent:curl/7.58.0
accept:*/*
I'm trying to build invoice pdf with wkhtmltopdf library and a symfony2 wrapper KnpSnappyBundle.
I want to add a footer on every page with the wkhtmltopdf option footer-html. In command-line, everything works fine, my pdf is built and a footer is repeated on every page. But when I use the bundle service :
$snappy = $this->get('knp_snappy.pdf');
$snappy->setOption('footer-html', 'http://www.google.com');
$content = $snappy->getOutputFromHtml($html);
return new Response(
$content,
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="file.pdf"'
)
);
it returns an error :
The exit status code '1' says something went wrong:
stderr: "Loading pages (1/6)
[> ] 0%
[======> ] 10%
Error: Failed loading page http://www.google.com (sometimes it will work just to ignore this error with --load-error-handling ignore)
Exit with code 1 due to network error: HostNotFoundError
"
stdout: ""
command: C:\wkhtmltopdf\bin\wkhtmltopdf.exe --lowquality --dpi "300" --margin-bottom "0" --margin-left "0" --margin-right "0" --margin-top "0" --page-size "A4" --background --encoding "utf-8" --footer-html "http://www.google.com" "http://www.google.com" "test.pdf".
Did any already succeed in using footer-html option with KnpSnappyBundle ?
I think the problem is with this line:
$snappy->setOption('footer-html', 'http://www.google.com');
The second argument in the setOption() method is looking for a html file on the local filesystem. As it is written currently it would try to include the Google homepage as a footer on every page of your pdf. Try something like this instead:
$snappy->setOption('footer-html', 'path/to/footer.html');
$snappy->getOutput('https://google.com');
Be aware that the .html is important, non-standard extension names like .phtml will cause errors.
Usually, I use the following structure to send POST request with contents of varchar2 and numbers .. etc.
content := '{"Original File Name":"'||V_HOMEBANNER_1_EN_NAME(indx)||'"}';
url := 'https://api.appery.io/rest/1/db/Names';
req := utl_http.begin_request(url, 'POST',' HTTP/1.1');
UTL_HTTP.set_header(req, 'X-Appery-Database-Id', '5f2dac54b02cc6402dbe');
utl_http.set_header(req, 'content-type', 'application/json');
UTL_HTTP.set_header(req, 'X-Appery-Session-Token', sessionToken);
utl_http.set_header(req, 'Content-Length', LENGTH(content));
utl_http.write_text(req, content);
res := utl_http.get_response(req);
BEGIN
LOOP
utl_http.read_line(res, buffer);
END LOOP;
utl_http.end_response(res);
EXCEPTION
WHEN utl_http.end_of_body THEN
utl_http.end_response(res);
END;
And It works just fine. However, now I want to send/upload a blob files (images of jpg) into some MongoDB collection called 'Files' (so url := ttps://api.appery.io/rest/1/db/Files). The collection guide has the following cURL as a general advice :
curl -X POST \
-H "X-Appery-Database-Id: 5f2dac54b02cc6402dbe" \
-H "X-Appery-Session-Token: <session_token>" \
-H "Content-Type: <content_type>" \
--data-binary '<file_content>' \
https://api.appery.io/rest/1/db/files/<file_name>
But I could not translate this cURL into PL/SQL request. Specifically, the part (--data-binary '')
I have these BLOB files in Oracle table and they are stored with their names as follows:
+-----------+--------------+
| File_Name | File_content |
+-----------+--------------+
| PIC_1.jpg | BLOB |
| PIC_2.jpg | BLOB |
| PIC_3.jpg | BLOB |
+-----------+--------------+
My question, how to upload these images into the targeted URL?
Inspired by this blog advised by #JeffreyKemp, I go it working by only replacing write_text() with write_raw() in order to send content body as BLOB (Requested by the API).
The following code is the critical part of my function with the changes needed:
content := V_HOMEBANNER_1_EN(indx);
file_name := 'test.jpg';
url := 'https://api.appery.io/rest/1/db/files/'||file_name;
req := utl_http.begin_request(url, 'POST',' HTTP/1.1');
UTL_HTTP.set_header(req, 'X-Appery-Database-Id', '53fae4b02cc4021dbe');
UTL_HTTP.set_header(req, 'X-Appery-Session-Token', sessionToken);
utl_http.set_header(req, 'content-type', 'image/jpeg');
req_length := DBMS_LOB.getlength(CONTENT);
DBMS_OUTPUT.PUT_LINE(req_length);
--IF MSG DATA UNDER 32KB LIMIT:
IF req_length <= 32767
THEN
begin
utl_http.set_header(req, 'Content-Length', req_length);
utl_http.write_raw(req, content);
exception
when others then
DBMS_OUTPUT.PUT_LINE(sqlerrm);
end;
--IF MSG DATA MORE THAN 32KB
ELSIF req_length >32767
THEN
BEGIN
DBMS_OUTPUT.PUT_LINE(req_length);
utl_http.set_header(req, 'Transfer-Encoding', 'Chunked');
WHILE (offset < req_length)
LOOP
DBMS_LOB.read(content, amount, offset, buffer);
utl_http.write_raw(req, buffer);
offset := offset + amount;
END LOOP;
exception
when others then
DBMS_OUTPUT.PUT_LINE(sqlerrm);
end;
END IF;
l_http_response := UTL_HTTP.get_response(req);
UTL_HTTP.read_text(l_http_response, response_body, 32767);
UTL_HTTP.end_response(l_http_response);
Tried and tested for both greater and smaller than 32KB images/jpg.