Cannot transcode invalid UTF-8 JSON text to UTF-16 string - f#

Given the function:
let postData url (payload:JObject) =
Http.RequestString
(url,
headers = [HttpRequestHeaders.ContentType HttpContentTypes.Json],
body = HttpRequestBody.TextRequest (payload |> string))
I am unable to send the data to the backend as it fails with the error:
AutoMapper.AutoMapperMappingException: Error mapping types.
---> System.InvalidOperationException: Cannot transcode invalid UTF-8 JSON text to UTF-16 string.
---> System.Text.DecoderFallbackException: Unable to translate bytes [E9] at index 629 from specified code page to Unicode.
at System.Text.DecoderExceptionFallbackBuffer.Throw(Byte[] bytesUnknown, Int32 index)
at System.Text.DecoderExceptionFallbackBuffer.Fallback(Byte[] bytesUnknown, Int32 index)
at System.Text.Encoding.GetCharCountWithFallback(ReadOnlySpan`1 bytes, Int32 originalBytesLength, DecoderNLS decoder)
at System.Text.Encoding.GetCharCountWithFallback(Byte* pBytesOriginal, Int32 originalByteCount, Int32 bytesConsumedSoFar)
at System.Text.UTF8Encoding.GetCharCount(Byte* bytes, Int32 count)
at System.String.CreateStringFromEncoding(Byte* bytes, Int32 byteLength, Encoding encoding)
at System.Text.Encoding.GetString(ReadOnlySpan`1 bytes)
at System.Text.Json.JsonReaderHelper.TranscodeHelper(ReadOnlySpan`1 utf8Unescaped)
--- End of inner exception stack trace ---
at System.Text.Json.JsonReaderHelper.TranscodeHelper(ReadOnlySpan`1 utf8Unescaped)
at System.Text.Json.JsonDocument.GetRawValueAsString(Int32 index)
at System.Text.Json.JsonElement.ToString()
at lambda_method(Closure , CreateContainerCommand , Container , ResolutionContext )
--- End of inner exception stack trace ---
at lambda_method(Closure , CreateContainerCommand , Container , ResolutionContext )
at lambda_method(Closure , Object , Object , ResolutionContext )
BUT the same request with the same body works just fine from Postman. What am I missing?
btw. printfn of payload |> string also prints the object just fine

Related

Google Closure Compile REST Api suddenly Throws Error 405 "Method not allowed"

I've been using Closure as part of y application to compress Javascript for a while now, but just started getting an error 405 - Method not allowed
My code as below was working for a couple of years, but has now stopped.
NOTE: this is not called frequently, just when ever any changes are detected in the Javascript files in my application.
I get no further error information from the Closure app than this.
Obviously this code performs a POST operation. If I use the form found here https://closure-compiler.appspot.com/home, it works, but if I either browser to the URL or use my code I get Error 405, it's almost as if my code is trying a GET method... but it's not...
Any ideas?
Public Class Closure
Property OriginalCode As String
Property CompiledCode As String
Property Errors As ArrayList
Property Warnings As ArrayList
Property Statistics As Dictionary(Of String, Object)
Public Sub New(Input As String, Optional CompliationLevel As String = "SIMPLE_OPTIMIZATIONS")
Dim _HttpWebRequest As HttpWebRequest
Dim _Result As StringBuilder
Dim ClosureWebServiceURL As String = "http://closure-compiler.appspot.com/compile?"
Dim ClosureWebServicePOSTData As String = "output_format=json&output_info=compiled_code" &
"&output_info=warnings" &
"&output_info=errors" &
"&output_info=statistics" &
"&compilation_level=" & CompliationLevel & "" &
"&warning_level=default" &
"&js_code={0}"
'// Create the POST data
Dim Data = String.Format(ClosureWebServicePOSTData, HttpUtility.UrlEncode(Input))
_Result = New StringBuilder
_HttpWebRequest = DirectCast(WebRequest.Create(ClosureWebServiceURL), HttpWebRequest)
_HttpWebRequest.Method = "POST"
_HttpWebRequest.ContentType = "application/x-www-form-urlencoded"
'//Set the content length to the length of the data. This might need to change if you're using characters that take more than 256 bytes
_HttpWebRequest.ContentLength = Data.Length
'//Write the request stream
Using SW As New StreamWriter(_HttpWebRequest.GetRequestStream())
SW.Write(Data)
End Using
Try
Dim response As WebResponse = _HttpWebRequest.GetResponse()
Using responseStream As Stream = response.GetResponseStream
Dim encoding As Encoding = System.Text.Encoding.GetEncoding("utf-8")
Using readStream As New StreamReader(responseStream, encoding)
Dim read(256) As Char
Dim count As Integer = readStream.Read(read, 0, 256)
While count > 0
Dim str As New String(read, 0, count)
_Result.Append(str)
count = readStream.Read(read, 0, 256)
End While
End Using
End Using
Dim js As New JavaScriptSerializer
js.MaxJsonLength = Int32.MaxValue
Dim d As Dictionary(Of String, Object) = js.Deserialize(Of Dictionary(Of String, Object))(_Result.ToString())
Me.CompiledCode = d.NullKey("compiledCode")
Me.Warnings = TryCast(d.NullKey("warnings"), ArrayList)
Me.Errors = TryCast(d.NullKey("errors"), ArrayList)
Me.Statistics = TryCast(d.NullKey("statistics"), Dictionary(Of String, Object))
Catch ex As Exception
Me.CompiledCode = ""
If Me.Errors Is Nothing Then
Dim er As New List(Of String)
er.Add(ex.ToString())
Me.Errors = New ArrayList(er)
Else
Me.Errors.Add(ex.ToString())
End If
End Try
Me.OriginalCode = Input
End Sub
End Class
Closure REST api is redirecting to https, you may want to try to POST to "https://closure-compiler.appspot.com/compile" directly in order to avoid redirection.

Crystal-Lang C-Binding struct doesn't seem to pass null value

I'm currently trying to add c14n support for crystal-lang using a c-binding with libxml2. I've successfully been able to use xmlC14NDocSave to save the canonical xml to a file. The problem I'm having is with the xmlOutputBufferPtr for both xmlC14NDocSaveTo and xmlC14NExecute.
The error I receive is (Mac and Linux)
xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output
The documentation states
this buffer MUST have encoder==NULL because C14N requires UTF-8 output
in src/C14N/lib_C14N.cr I have the following code
type CharEncodingHandler = Void*
type Buff = Void*
#type OutputBuffer = Void*
struct OutputBuffer
context : Void*
writecallback : OutputWriteCallback
closecallback : OutputCloseCallback
encoder : CharEncodingHandler
buffer : Buff
conv : Buff
written : Int32
error : Int32
end
....
fun xmlC14NDocSaveTo(doc : LibXML::Node*, nodes : LibXML::NodeSet*, mode : Mode, inclusive_ns_prefixes : UInt8**, with_comments : Int32, buf : OutputBuffer*) : Int32
fun xmlC14NExecute(doc : LibXML::Node*, is_visible_callback : IsVisibleCallback, user_data : Void*, mode : Mode, inclusive_ns_prefixes : UInt8**, with_comments : Int32, buf : OutputBuffer*) : Int32
In src/C14N.cr
output = XML::LibC14N::OutputBuffer.new
p output.encoder
XML::LibC14N.xmlC14NDocSaveTo(#xml, nil, #mode, nil, 0, out output)
The results of the p ouput.encoder are Pointer(Void).null so It appears the value is null.
The c14n.c function is just checking null on the buf->encoder struct
if (buf->encoder != NULL) {
xmlGenericError(xmlGenericErrorContext,
"xmlC14NExecute: output buffer encoder != NULL but C14N requires UTF8 output\n");
return (-1);
}
Any help would be appreciated, the code can be found on my github account. clone and run crystal spec
Don't specify out output, that simply reserves a block of memory of the size of the struct on the stack and passes a pointer to it. As of Crystal 0.7.6 it's not zeroed it out, so you pass garbage.
Using output = XML::LibC14N::OutputBuffer.new is already the first right step, since that does zero out the memory. Now to pass it, simply replace out output with pointerof(output).

Arithmetic operation resulted in an overflow - TweetSharp

Code:
TwitterService twitter = new TwitterService(oauth_consumer_key, oauth_consumer_secret);
twitter.AuthenticateWith(oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret);
TwitterSearchResult res = twitter.Search(new SearchOptions { Q = hashtag, Count = 10, MaxId = max_id });
Error:
Arithmetic operation resulted in an overflow
Stacktrace:
at Newtonsoft.Json.Utilities.ConvertUtils.Int32Parse(Char[] chars, Int32 start, Int32 length)
at Newtonsoft.Json.JsonTextReader.ParseNumber()
at Newtonsoft.Json.JsonTextReader.ParseValue()
at Newtonsoft.Json.JsonTextReader.ReadInternal()
at Newtonsoft.Json.JsonReader.ReadAsInt32Internal()
at Newtonsoft.Json.JsonTextReader.ReadAsInt32()
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)
I want to get all the tweets with particular keyword in it. Sometimes the response is proper while sometime i get this error.

GLib-GIO-CRITICAL error While invoking a method on dbus interface

I am using lgi dynamic Lua binding to GObject based libraries to communicate between applications through dbus.Below is some portion of output of the dbus-send command for the method_call of my application.
method return sender=:1.2 -> dest=:1.19 reply_serial=2
array [
dict entry(
string "XYZ"
variant int32 1
)
dict entry(
string "ABC"
variant int32 0
)
dict entry(
string "EFG"
variant string "str"
)
dict entry(
string "HIJ"
variant string "8c011401-4836-4889-8fa5-32ddb894a97a"
)
dict entry(
string "KLM"
variant string "dummy"
)
Now I want to set the "DEF" value in the above output which has signature as 'a{sv}'. Below is the code I wrote to do the same:
local lgi=require 'lgi'
local Gio=lgi.require 'Gio'
local GLib=lgi.GLib
local db=require 'debugger'
local factory=function()
return Gio.bus_get_sync(Gio.BusType.SYSTEM)
end
local bus=factory()
local j=GLib.Variant.new('s','value')
local k2=GLib.Variant('a{sv}',{DEF=j})
str,err=bus:call_sync('org.destination','/my/application/obj_path','org.destination.path','Method_name',k2,nil,Gio.DBusConnectionFlags.NONE,-1)
if err then
print(err)
else
print(str)
end
When I run the above script I am facing the following error:
(process:19120): GLib-GIO-CRITICAL **: g_dbus_connection_call_sync_internal: assertion '(parameters == NULL) || g_variant_is_of_type (parameters, G_VARIANT_TYPE_TUPLE)' failed.
How to set the above value ? Where am I doing wrong ? Can someone help me ?
Thanks in advance.
The error tells you pretty clearly what the problem is. The "parameter" argument to call_sync (g_dbus_connection_call_sync in the C API) needs to be a tuple. You have provided a dictionary. Change k2 so that it is a tuple variant.

Operation could destabilize runtime, works local, not on server

Here is my Action:
Imports PagedList
<EmployeeAuthorize()>
Function SearchFoods(Optional ByVal date1 As String = "", Optional ByVal keyword As String = "", Optional page As Integer = 1) As ActionResult
If String.IsNullOrEmpty(date1) Then
date1 = Date.Now
End If
If String.IsNullOrEmpty(keyword) Then
keyword = Nothing
End If
Dim food = db.Tbl_Foods.Where(Function(x) x.Shrt_Desc.Contains(keyword)).OrderBy(Function(x) x.Food_ID).ToList
For Each item In food
item.Shrt_Desc = item.Shrt_Desc.Replace(",", ", ")
Next
ViewBag.MyDate = date1
ViewBag.MyKeyword = keyword
' set the page size and number
Dim pageSize = 20
Dim pageNumber = page
TempData("CurrentPage") = "My Wellness"
TempData("CurrentWellnessPage") = "Food Log"
Return View("", "_FinalWellnessSubPageLayout", food.ToPagedList(pageNumber, pageSize))
End Function
I believe this is caused, somehow, by the "ToPagedList" since this is the first time I am using it. It works fine on local, but not when I publish to the server. The stack trace looks like this:
[VerificationException: Operation could destabilize the runtime.]
PagedList.PagedList1..ctor(IEnumerable1 superset, Int32 pageNumber,
Int32 pageSize) +0
PagedList.PagedListExtensions.ToPagedList(IEnumerable`1 superset,
Int32 pageNumber, Int32 pageSize) +62
It also says:
An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.
What is the exception? How can I find what exception "occurred during the execution?"
Does anyone know how to fix this error on the server? Thank you.
I also faced the same problem with IIS 7.5 it was working fine with IIS 6.0 so I reinstalled the Pagedlist package and it worked like a charm.

Resources