How to execute a HttpRequest of type HttpRequestMode.Post

No need to, i don’t think any platform currently sets them… will fix all — retesting with next would be appreciated though.

Absolutely I’m checking that, thank you!

@mh I have updated Sugar to the latest commit 74be2596d003349e5c6b9ee8fe4e58d8e60766a6 and checkout out, it seems the headers are not modified yet:

Time: Mon, 22 Feb 16 07:22:34 -0800
Source ip: 5.90.77.25

Headers (Some may be inserted by server)
REQUEST_URI = /post.php?dir=swift-promise
QUERY_STRING = dir=swift-promise
REQUEST_METHOD = POST
GATEWAY_INTERFACE = CGI/1.1
REMOTE_PORT = 1221
REMOTE_ADDR = 5.90.77.25
HTTP_ACCEPT_ENCODING = gzip, deflate
CONTENT_LENGTH = 21
HTTP_ACCEPT_LANGUAGE = it-it
HTTP_USER_AGENT = iOSSilverSampleApp/1.0 CFNetwork/758.2.8 Darwin/15.0.0
HTTP_ACCEPT = */*
HTTP_CONNECTION = close
CONTENT_TYPE = application/x-www-form-urlencoded
HTTP_HOST = posttestserver.com
SSL_TLS_SNI = posttestserver.com
HTTPS = on
UNIQUE_ID = VssnukBaMGUAAEA1LckAAAAH
REQUEST_TIME_FLOAT = 1456154554.4659
REQUEST_TIME = 1456154554

Post Params:
key: 'name' value: 'test'
key: 'value' value: 'pippo'
Empty post body.

Upload contains PUT data:
name=test&value=pippo

setting

request.Headers["User-Agent"]="switft-example";

Yes, this fix isn’t in yet, sorry. Didn’t get chance to fully do it for all platforms yet.

1 Like

Are you mixing threads here? is this about HTTP Headers or JSON?

:joy: I just wrote a response supposed to be in Sugar.json.JsonObject.getKeys: java.lang.ClassCastException here, don’t tell me why…removing!

I just committed the fixes for setting the HTTP headers, btw. Along with some other HTTP improvements. Testing/feedback appreciated!

1 Like

Great!

So just pulled and updated. So on iOS it works now, I can see that headers were modified:

Time: Tue, 01 Mar 16 10:48:08 -0800
Source ip: 89.97.90.230

Headers (Some may be inserted by server)
REQUEST_URI = /post.php?dir=swift-promise
QUERY_STRING = dir=swift-promise
REQUEST_METHOD = POST
GATEWAY_INTERFACE = CGI/1.1
REMOTE_PORT = 50620
REMOTE_ADDR = 89.97.90.230
HTTP_ACCEPT_ENCODING = gzip, deflate
HTTP_ACCEPT_LANGUAGE = it-it
CONTENT_LENGTH = 48
HTTP_USER_AGENT = mxmevents
HTTP_ACCEPT = */*
HTTP_CONNECTION = close
CONTENT_TYPE = application/x-www-form-urlencoded
HTTP_HOST = posttestserver.com
SSL_TLS_SNI = posttestserver.com
HTTPS = on
UNIQUE_ID = VtXj6EBaMGUAAAn8BjcAAAAc
REQUEST_TIME_FLOAT = 1456858088.6908
REQUEST_TIME = 1456858088

Post Params:
key: 'item' value: '{
    name = track;
}'
key: 'timestamp' value: '1456858087'
Empty post body.

Upload contains PUT data:
&item={
    name = track;
}&timestamp=1456858087

On Android I get a verification failed error:

03-01 19:50:11.513 21513-21900/musixmatch.com.sampleandroidapp I/art: Verification error in sugar.HttpResponse sugar.Http.ExecuteRequestSynchronous(sugar.HttpRequest)
03-01 19:50:11.514 21513-21900/musixmatch.com.sampleandroidapp I/art: sugar.HttpResponse sugar.Http.ExecuteRequestSynchronous(sugar.HttpRequest) failed to verify: sugar.HttpResponse sugar.Http.ExecuteRequestSynchronous(sugar.HttpRequest): [0x82] thrown class Precise Reference: sugar.HttpResponse not instanceof Throwable
03-01 19:50:11.520 21513-21900/musixmatch.com.sampleandroidapp W/art: Verification failed on class sugar.Http in /data/app/musixmatch.com.sampleandroidapp-2/base.apk because: Verifier rejected class sugar.Http due to bad method sugar.HttpResponse sugar.Http.ExecuteRequestSynchronous(sugar.HttpRequest)

I’m trying to check if this could be due to something else in the code, but not sure. The code was:

public func getJsonObject(var url: String, success: (response:MXMEvent?) ->(), error: (response:Exception?) ->()  ) -> () {
		let jsonCallback: HttpContentResponseBlock<Sugar.Json.JsonDocument!>! = { response in 
			if response.Success {
				
				let jsonObject:Sugar.Json.JsonObject = response.Content.RootObject;
				
				let rndIndex=(Sugar.Random()).NextInt();
				let key="USER_"+Sugar.Convert.ToString(rndIndex,10);
				let now = DateTime.UtcNow
				let unixMsec = (now.Ticks - DateTime.TicksSince1970 ) / TimeSpan.TicksPerSecond;
				
				var jsonString = "{}";
				do {
					jsonString = try jsonObject.ToString();
				} catch error {
				}
				
				var cacheObject:MXMEvent = MXMEvent(key:key, value:jsonString, timestamp: Convert.ToString( unixMsec, 10 ) );
				cacheObject.map( jsonString );
				success( cacheObject );
				
			}
			else {
				error(response.Exception);
			}
		}
		let signedUrl=getSignedUrl(url);
		Http.ExecuteRequestAsJson( Url(url), jsonCallback)
	} //testGetJson

and the error referenced was in Http.ExecuteRequestAsJson( Url(url), jsonCallback)

Thanks, logged as bugs://74652

1 Like

That verify error is weird. I’ll take a look.

@ck If it helps, this happens for every HttpRequest like:

Http.ExecuteRequestAsJson( Url(url), jsonCallback)

or Http.ExecuteRequest, were this one this was for a HttpRequestMode.Post.

So this:

import Sugar

let jsonCallback: HttpContentResponseBlock<Sugar.Json.JsonDocument!>! = {
  response in 
    writeLn(response)
}
Http.ExecuteRequestAsJson( Url("http://blogname.blogspot.com/feeds/posts/default?alt=json-in-script&callback=myFunc"), jsonCallback)

writeLn("meh")

in java should have failed? Or only on Android? Seems to work on Java.

@ck It fails in Android.

Just tried this:

import java.util
import android.app
import android.content
import android.os
import android.util
import android.view
import android.widget
import Sugar

public class MainActivity: Activity {

	public override func onCreate(savedInstanceState: Bundle!) {
		super.onCreate(savedInstanceState)
		ContentView = R.layout.main


        
let jsonCallback: HttpContentResponseBlock<Sugar.Json.JsonDocument!>! = {
  response in 
    writeLn(response)
}
Http.ExecuteRequestAsJson( Url("http://blogname.blogspot.com/feeds/posts/default?alt=json-in-script&callback=myFunc"), jsonCallback)

writeLn("meh")

	}
}

with:


  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

and a Jellybean (4.2) emulator. Works fine? (Last beta)

@ck thanks let me try to figure out what’s going on.

Im running: Fire (8.3.90.1951), Sugar commit e190f0243ce880db5eebaeab86f948a7aa9e5178, testing on Nexus 7, Android 6.0.1.

What I see with this set up is the issue reported here: Fire Beta: Unable to run template Android Application (Swift)

so that Fire get stuck before deploy the Android Application template to the device:

Installing org.me.androidsilversampleapp Calling adb -s 0a21584f install -r "/Users/admin/Projects/MusiXmatch/MXMPortableNETLib/Samples/AndroidSilverSampleApp/./bin/Release/AndroidSilverSampleApp.apk" The process failed to start with the following error:

Then, having no option, I try with AndroidStudio 1.5.1, API Level 23, Marshmallow.
Here before pulling Sugar to latest develop commit (I presume) I get this verifier error:

03-03 14:35:07.048 24771-24732/musixmatch.com.sampleandroidapp W/art: Verification failed on class sugar.Http in /data/app/musixmatch.com.sampleandroidapp-2/base.apk because: Verifier rejected class sugar.Http due to bad method sugar.HttpResponse sugar.Http.ExecuteRequestSynchronous(sugar.HttpRequest)

Maybe, bu this is my supposition, is that those two errors are related in some way.

I don’t think the 'stcuk during deploy" is related. Can you send the exact project you used?

Let me know a fresh install, kill adb, etc since it could be a wrong or misconfigured environment issue first. Back with the project asap.

Sorry I meant unrelated.

@ck So I have

  • killed adb
  • restared the mac and the device;
  • installed Xamarin emulator just to be sure it was something not related to my configuration of AndroidStudio VDs;

tried now in this first test my configuration with

Xamarin Android Player / Nexus 7 Marshmallow

and I get the same

03-03 15:44:54.625 1706-1905/musixmatch.com.sampleandroidapp I/art: Verification error in sugar.HttpResponse sugar.Http.ExecuteRequestSynchronous(sugar.HttpRequest)
03-03 15:44:54.625 1706-1905/musixmatch.com.sampleandroidapp I/art: sugar.HttpResponse sugar.Http.ExecuteRequestSynchronous(sugar.HttpRequest) failed to verify: sugar.HttpResponse sugar.Http.ExecuteRequestSynchronous(sugar.HttpRequest): [0x82] thrown class Precise Reference: sugar.HttpResponse not instanceof Throwable
03-03 15:44:54.625 1706-1905/musixmatch.com.sampleandroidapp W/art: Verification failed on class sugar.Http in /data/app/musixmatch.com.sampleandroidapp-1/base.apk because: Verifier rejected class sugar.Http due to bad method sugar.HttpResponse sugar.Http.ExecuteRequestSynchronous(sugar.HttpRequest)

The application is here:

https://github.com/loretoparisi/swift-promise-example/tree/master/SampleAndroidApp

Doing further tests.

@ck Which was your API level?
Btw, I have found which is the issue with debugger (see Fire Beta: Unable to run template Android Application (Swift)).
Now looking at this error again…