Passing long parameters to REST service avoiding 414 error


#1

Continuing the discussion from Load & Retrieve Images into/from DB by REST service:

I managed to have an image from a file and convert data into base64; however, when I try to send a REST request, it throws a 414 error (too long a request). How can I send a request w/o hitting this problem?
Actually, “file” variable starts with headers(“data:image/jpeg;base64,”) to be stripped in code by the REST service

BTW foto has a length of 43445 Bytes

Function btnLoadImg_onclick()
  debugger
  sentFlag=1
  btnLoadImg.hidden=True
  txtFile2Load.hidden=True
  Dim foto=PictureBox4.src
  Dim fotolen=foto.length
  URL="http://"+txtSvr.value+"/RestSvc4Gescom/UpdateDati.svc/insertfoto/?uid="+txtUserId.value+"&pwd="+txtPassword.value
  URL+="&dioc="+codDiocesi4+"&coda="+coda+"&foto="+foto
  encURL=encodeURI(URL)
  req=Ajax(encURL,"POST","")
  If req.status=200 Then
      lblRespAgg.textContent=req.responseText
      btnAggiorna.disabled=True
  Else
      lblRespAgg.textContent="Errore "+req.status
  End If
End Function

#2

Have another look at the sample code in Ajax Made Simple:
https://wiki.nsbasic.com/Ajax_made_Simple#Using_AJAX_to_get_save_a_picture_to_your_server

Notice that the data on a POST is passed in the third parameter, not part of the URL.


#3

Lot of Thanks, George, 4 your reply. Of course, it’s very useful. However, here it rises another pitfall! In my REST Service code, I play with Interface where i CANNOT place methods, and Implemented function from where I have no access to interface data; so how can I get POST data?

from iUpdateDati.vb

<OperationContract()> <WebInvoke(Method:="POST", UriTemplate:="insertfoto/?uid={userid}&pwd={password}&dioc={dioc}&coda={coda}&foto={foto}", ResponseFormat:=WebMessageFormat.Json)>
Function InsertFoto(userid As String, password As String, dioc As String, coda As String, foto As String) As String

from UpdateDati.vb

Public Function InsertFoto(userid As String, password As String, dioc As String, coda As String, foto As String) As String Implements IUpdateDati.InsertFoto
    Dim img As Byte() = Convert.FromBase64String(foto)
    Dim sqnCs = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("CamminoSqlDB").ConnectionString
    Dim sqc As New SqlConnection(sqnCs)
    Dim cmdUpFoto As New SqlCommand("UPDATE Anagrafica SET Foto = @Foto WHERE CodDiocesi = " + CStr(dioc) + " AND CodAnagrafica = " + CStr(coda), sqc)
    Dim parFoto As New SqlParameter("@Foto", System.Data.SqlDbType.Image)
    parFoto.Value = img
    cmdUpFoto.Parameters.Add(parFoto)
    sqc.Open()
    Dim rkUpd = cmdUpFoto.ExecuteNonQuery()
    sqc.Close()
    Return "Foto aggiornata correttamente"
End Function

#4

Forgive me if this is a little OT. I’m replying by myself 'cause maybe I can help someone else.

If I POST data with 3rd parameter of Ajax() call, in REST service it’s mandatory to put a stream parameter in implementing function (and of course in the Interface declaration). So here’s my code:

<OperationContract()> <WebInvoke(Method:="POST", UriTemplate:="insertfoto/?uid={userid}&pwd={password}&dioc={dioc}&coda={coda}", ResponseFormat:=WebMessageFormat.Json)>
Function InsertFoto(userid As String, password As String, dioc As String, coda As String, foto As Stream) As String

Public Function InsertFoto(userid As String, password As String, dioc As String, coda As String, foto As Stream) As String Implements IUpdateDati.InsertFoto
    Dim leggi As String = New StreamReader(foto).ReadToEnd
    Dim img As Byte() = Convert.FromBase64String(leggi)
    Dim sqnCs = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("CamminoSqlDB").ConnectionString
    Dim sqc As New SqlConnection(sqnCs)
    Dim cmdUpFoto As New SqlCommand("UPDATE Anagrafica SET Foto = @Foto WHERE CodDiocesi = " + CStr(dioc) + " AND CodAnagrafica = " + CStr(coda), sqc)
    Dim parFoto As New SqlParameter("@Foto", System.Data.SqlDbType.Image)
    parFoto.Value = img
    cmdUpFoto.Parameters.Add(parFoto)
    sqc.Open()
    Dim rkUpd = cmdUpFoto.ExecuteNonQuery()
    sqc.Close()
    Return "Foto aggiornata correttamente"
End Function

Finally, this is my VB code (pls note that I had to use PictureBox4.src instead of .toDataURL):

  Dim foto=PictureBox4.src
  URL="http://"+txtSvr.value+"/RestSvc4Gescom/UpdateDati.svc/insertfoto/?uid="+txtUserId.value+"&pwd="+txtPassword.value+"&dioc="+codDiocesi4+"&coda="+coda
  encURL=encodeURI(URL)
  req=Ajax(encURL,"POST",foto)

Image data base
What is the size limit to send a photo w/Ajax POST and don't hit a 413 error?