Jump to content

Output JSON for Alamofire


AndZyk
 Share

Recommended Posts

Hello,

currently I am trying to develop my first iPad app with Xcode. For handling the data on a remote server I want to use ProcessWire as middleware. For synchronizing the data I am trying the Xcode framework Alamofire, which has more convenient functions for handling such requests, especially for a beginner in Swift like me.

Now I am wondering how I should output my JSON objects in my template files the right way for Alamofire to recognize them. So far I tried the following:

<?php

$data = array(
	'test' => 'Hello World!'
);

header('Content-Type: application/json');

echo json_encode($data);

But when I build my app and request the JSON after loading the first view, I don't get a response. Maybe I am missing something. The example URL from GitHub works as expected.

Do I have to be more specific with my JSON objects and do you think this is a good solution for handling data in general?

Regards, Andreas

Link to comment
Share on other sites

I would say JSON is a good solution if Alamofire likes or requires it..."just" use the format most convenient for your app or the framework..pw doesn't care ;)

I don't know why you wouldn't get any output, just tested your code and I'm using something similar for sitemap.xml as suggested by Ryan

Nowadays (don't remember which version introduced this feature) you don't need to set the header in your template yourself you could set the template in the backend to json ;)

what pw version are you running and how does your template setup look like? do you have a pre- and appended template file (think this is called delayed output)?
But probably not if it's just you app backend and not a "normal" website..

And what do you get when you request this page from your browser?

  • Like 1
Link to comment
Share on other sites

You could try out my rest api helper

https://github.com/NinjasCL/pw-rest

Alamo Fire as AFNetworking
its format agnostic, so it could easily adapt to your api design

I recommend you to follow http://jsonapi.org principles.

also reading this book https://leanpub.com/build-apis-you-wont-hate

and this book https://leanpub.com/iosappswithrest

could help you start out.

If you need something done quick maybe using Parse Server SDK
its more appropriate for a middleware (you can call PW later with that)

https://parseplatform.github.io

  • Like 3
Link to comment
Share on other sites

Thank you both for your answers. Sorry for my late response, but unfortunately I hadn't got the time to test until now.

On 5.8.2016 at 0:26 AM, Can said:

I would say JSON is a good solution if Alamofire likes or requires it..."just" use the format most convenient for your app or the framework..pw doesn't care ;)

I don't know why you wouldn't get any output, just tested your code and I'm using something similar for sitemap.xml as suggested by Ryan

Nowadays (don't remember which version introduced this feature) you don't need to set the header in your template yourself you could set the template in the backend to json ;)

what pw version are you running and how does your template setup look like? do you have a pre- and appended template file (think this is called delayed output)?
But probably not if it's just you app backend and not a "normal" website..

And what do you get when you request this page from your browser?

@Can

Exactly that is the reason why I would prefer PW as middleware. Because it doesn't care. ;)

Turns out, I couldn't get any response, because I requested the JSON from a PW installation hosted on my local machine using MAMP. After uploading the installation to a server I get a response.

I forgot about, that you could set the header content-type on a template basis. That is easier than setting it in the template file.

I am running PW 3.0.28 devns and my template setup looks like the above example for now. Right now I am just experimenting if PW would fit as a good solution.

On 5.8.2016 at 2:29 PM, clsource said:

You could try out my rest api helper

https://github.com/NinjasCL/pw-rest

Alamo Fire as AFNetworking
its format agnostic, so it could easily adapt to your api design

I recommend you to follow http://jsonapi.org principles.

also reading this book https://leanpub.com/build-apis-you-wont-hate

and this book https://leanpub.com/iosappswithrest

could help you start out.

If you need something done quick maybe using Parse Server SDK
its more appropriate for a middleware (you can call PW later with that)

https://parseplatform.github.io

@clsource

Thank you for the useful resources. I haven't done anything with REST yet, but will have a closer look.

Regards, Andreas

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...